1

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.

2
  • 2
    Some relevant code may be helpful and demonstrate what you tried already. Commented Apr 23, 2015 at 5:26
  • 1
    looks like duplicated stackoverflow.com/questions/1228539/… Commented Apr 23, 2015 at 5:46

5 Answers 5

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

OP stated that the data is in a List<T> - how does this help them bind the List<T> to the DataGridView?
2

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

The DataGridView class does not have DataBind method.
0

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

dataGridView1.DataSource = myCollection;

Comments

0

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

Comments

-1

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

The DataGridView class does not have DataBind method. Also, what's the point in copying the List another array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.