I already have rows and columns for dataGridView in c# widows form application. How can I display the data into `datagridview..whole the data are in a list. Now, I want to load the list into datagridview.. I am new to c# and would appreciate any help.
-
2Some relevant code may be helpful and demonstrate what you tried already.AndroidEx– AndroidEx2015-04-23 05:26:56 +00:00Commented Apr 23, 2015 at 5:26
-
1looks like duplicated stackoverflow.com/questions/1228539/…Ilia Maskov– Ilia Maskov2015-04-23 05:46:07 +00:00Commented Apr 23, 2015 at 5:46
Add a comment
|
5 Answers
In your question you didn't mention which type of list you use. Try to do this using data table.
DataTable dt = new DataTable();
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
foreach(var oItem in YourList)
{
dt.Rows.Add(new object[] { oItem.FirstName, oItem.LastName });
}
myDataGridView.DataSource = dt;
1 Comment
Tim
OP stated that the data is in a
List<T> - how does this help them bind the List<T> to the DataGridView?sounds like you want to use a list as the datasource.
List<myObject> oblst = new List<myObject>;
//insert into the list
datagridview.DataSource = oblst;
Removed datagridview.DataBind();
1 Comment
sloth
The
DataGridView class does not have DataBind method.You can use ToArray() to convert your list. Say if your DataGridView named as yourdataGridView and the available list named as yourList then
yourdataGridView.DataSource = yourList.ToArray();
1 Comment
sloth
The
DataGridView class does not have DataBind method. Also, what's the point in copying the List another array?