DataGrid and ArrayList binding

K

kids_pro

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
 
C

Cor Ligthert [MVP]

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
 
B

Bart Mermuys

Hi,

kids_pro said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top