tags:

views:

96

answers:

5

how do I bind a DataGrid to a collection ? (e.g : List ).

i used this :

Datagrid dg = new Datagrid();
dg.DataSource = myCollection;
dg.DataBind();

but after this, the Datagrid still doesn't display the data from the collection.

+4  A: 

The same way as you bind a datagrid to a DataTable/DataSet. Your object properties will behave like column names when databinding.

DataGrid1.DataSource = myList;
DataGrid1.DataBind();
Steve Danner
+1  A: 
List<string> lst= new List<string>();

lst.Add("your string");

Datagrid dg = new Datagrid();
dg.DataSource=lst;
dg.DataBind();

disclaimer: I have not run this code, but this should give your a general idea

ram
A: 

Just set YourDataGrid.Datasource to the instance name of the List and call the YourDataGrid.DataBind() function.

klausbyskov
+1  A: 

You have to add your Datagrid object (dg) to your form.

this.Controls.Add(dg);
Tim Coker