Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
My task is to import contacts from Outlook.
user going to export the file in .csv format I have to parse that data and take firstname, lastname and Email from that file.

Part of that is this.. I am able to separate into each lines then tring to get create an array.

If I collect fields I want from that array and store it in an object
like Myobject.FistName="balaa"
Myobject.LastName = "jddk"
.Email ="dad@hakdj.com"

1) If I create a list those objects from the array can I pass that List<myobjects> to gridview as data source.. ???

2) While parsing from string array in the bottom code I have one more problem... the first element in the array was menu from the file not the values...

what I am saying is "fieldOnLine[0]" in the bottom code is MENU names(like FirstName, LastName.. Not the user details so I have to avoiid it. DO I have to use for loop instead, to avoid those values????


using (Stream s = file.InputStream)
{
  string[] numberOfUsers;
  var sr = new StreamReader(s);
  string contacts = sr.ReadToEnd();
  contacts = contacts.Replace("\r\n", "\n");
  string[] separateLines = contacts.ToString().Split('\n');
  foreach (string line in separateLines)
{
  numberOfUsers = line.Split(',');
  // now you can go through the field
  foreach( string fieldOnLine in numberOfUsers )
  {
    // process fieldOnLine here
  }
}





I know I am asking silly questions..

Thanks for your help ....
Posted
Updated 14-Oct-10 3:32am
v3
Comments
Ryan Gamal 14-Oct-10 10:37am    
Please can you clarify the format of your data, is it:

Firstname, LastName, Email
Bob, Hoskins, B@b.com
Frank, Sinatra, f@s.com

OR:

Firstname, Bob, LastName, Hoskins, Email, B'b.com
Firstname, Frank, LastName, Sinatra, Email, f@s.com
Jayadheer Reddy 14-Oct-10 20:51pm    
It's in the First format....

Firstname, LastName, Email
Bob, Hoskins, B@b.com
Frank, Sinatra, f@s.com

Yes dude , you are right , you can surely provide list of objects as the datasource , as gridview can handle any source that implements iEnumerable interface.
suppose you have a class named classA. then to provide list of objects of type classA. you can do something as below:
C#
List<classA> lst=new List<classA>();

// code to add items to list
GridView1.Datasource=lst;
GridView1.DataBind();

// If the Class classA has properties named Prop1 and Prop2 ,you should create columns(that can be boundfield , templatefield,etc.)and set the appropriate Property name in the data field.example

XML
< GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="Prop1" HeaderText="Property 1" />
                        <asp:TemplateField HeaderText="Property 2">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Prop2") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Prop2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

And yes you can use for loop for solving your second problem .
also you can use the same foreach loop with minor modification as shown below:
C#
using (Stream s = file.InputStream)
{
  string[] numberOfUsers;
  var sr = new StreamReader(s);
  string contacts = sr.ReadToEnd();
  contacts = contacts.Replace("\r\n", "\n");
  string[] separateLines = contacts.ToString().Split('\n');
  foreach (string line in separateLines)
{
  numberOfUsers = line.Split(',');
  // now you can go through the field
  int count=0;
  foreach( string fieldOnLine in numberOfUsers )
  {
    if(count>0)
    {
    // process fieldOnLine here
    }
    count++;
  }
}
 
Share this answer
 
v2
Comments
Sandeep Mewara 15-Oct-10 8:15am    
Just formatted your answer!
XML
Create a List<Issue> and set values to properties in Issue object. Here is a sample of binding Generic List to GridView:
The TeamCO class:

C#
public class TeamCO
{
 public TeamCO()
 {
 }
    private int _TeamId;
    private string _TeamName;
    public int TeamId
    {
        get { return _TeamId; }
        set { _TeamId = value; }
    }
    public string TeamName
    {
        get { return _TeamName; }
        set { _TeamName = value; }
    }
}
protected void Page_Load(object sender, EventArgs e)
    {
        List<TeamCO> myTeam = new List<TeamCO>();      //create Generic List
        TeamCO t1 = new TeamCO();      //initialize the List
        t1.TeamId = 1000;
        t1.TeamName = "Spurs";
        myTeam.Add(t1);
        TeamCO t2 = new TeamCO();
        t2.TeamId = 2000;
        t2.TeamName = "Lakers";
        myTeam.Add(t2);
        GridView1.DataSource = myTeam;     //bind to GridView
        GridView1.DataBind();
    }

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="NBA">
            <ItemTemplate>
                <%# Eval("TeamID")%>
                <br/>
                <%# Eval("TeamName")%>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
</asp:GridView>
Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900