kavidhaya
kavidhaya

Reputation: 19

How to load <list> data into dataGridView in c#

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.

Upvotes: 1

Views: 12753

Answers (5)

mBardos
mBardos

Reputation: 3067

If anyone is brought here by Google/etc, use a BindingList

Upvotes: 0

Muneeb Ajaz
Muneeb Ajaz

Reputation: 1

You can use any collection that implements IEnumerable and simply assign it as DataSource to your DataGridView , like

dataGridView1.DataSource = myCollection;

Upvotes: 0

Dominic Scanlan
Dominic Scanlan

Reputation: 1009

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();

Upvotes: 2

Dush
Dush

Reputation: 1365

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();

Upvotes: -1

Mehul Patel
Mehul Patel

Reputation: 1194

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;

Upvotes: 2

Related Questions