469,609 Members | 1,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

DataGrid and ArrayList binding

I created a simple Person class as following:

class Person{
private string fname;
private string lname;

public Person(string fname, string lname){
this.fname = fname;
this.lname = lname;
}

public string FirstName{
get{return this.fname;}
set{this.fname = value;}
}

public string LastName{
get{return this.lname;}
set{this.lname = value;}
}
}

-- I create Person ArrayList
ArrayList pList = new ArrayList();
pList.Add(new Person("Jame","C."));
pList.Add(new Person("Jonh","D."));

-- binding to datagrid
dataGrid1.DataSource = pList;

-- select data grid row and get back the selected Person object.
How can I complete this stage?

Regards,
Kids
Nov 17 '05 #1
2 1846
Kids,

If you use a datatable in C# for Net 1.1 than you get all functionality from
the datagrid.
With an arraylist you get that just partially.

To create a datatable in your sample is nothing more than

DataTable dt = new DataTable(Persons);
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Rows.LoadDataRow(new Object() {"Jame","C"},true);
dt.Rows.LoadDataRow(new Object() {"John","D"},true);
dataGrid1.DataSource = dt;

Watch typos, I have typed it in directly in this message without a layer.

I hope this helps,

Cor

"kids_pro" <ki******@yahoo.com> schreef in bericht
news:e%****************@TK2MSFTNGP12.phx.gbl...
I created a simple Person class as following:

class Person{
private string fname;
private string lname;

public Person(string fname, string lname){
this.fname = fname;
this.lname = lname;
}
public string FirstName{
get{return this.fname;}
set{this.fname = value;}
}

public string LastName{
get{return this.lname;}
set{this.lname = value;}
}
}

-- I create Person ArrayList
ArrayList pList = new ArrayList();
pList.Add(new Person("Jame","C."));
pList.Add(new Person("Jonh","D."));

-- binding to datagrid
dataGrid1.DataSource = pList;

-- select data grid row and get back the selected Person object.
How can I complete this stage?

Regards,
Kids

Nov 17 '05 #2
Hi,

"kids_pro" <ki******@yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP12.phx.gbl...
I created a simple Person class as following:

class Person{
private string fname;
private string lname;

public Person(string fname, string lname){
this.fname = fname;
this.lname = lname;
}
public string FirstName{
get{return this.fname;}
set{this.fname = value;}
}

public string LastName{
get{return this.lname;}
set{this.lname = value;}
}
}

-- I create Person ArrayList
ArrayList pList = new ArrayList();
pList.Add(new Person("Jame","C."));
pList.Add(new Person("Jonh","D."));

-- binding to datagrid
dataGrid1.DataSource = pList;

-- select data grid row and get back the selected Person object.
How can I complete this stage?
Get the selected person:

Person p = (Person) dataGrid1.BindingContext[pList].Current;

HTH,
Greetings


Regards,
Kids

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.