How to Bind DataGridView to List Of Items: C#

Datagridview is the common control to bind our records in windows forms. I have bound this datagridview to dataset and in this article I will bind a list of student class’s data. I will use the same student class as in my previous post.

Drag-n-Drop a DataGridView on the form and write the following C# line of code in the constructor of the form.
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = stuList;
}

Before running the project, just sure that you have inserted some items in the stuList. To insert some records we can use an in-built Add function of the list. Write the following code to insert records:
stuList.Add(new Student() { Name = "Jacob", Age = 29, City = "London", Dob = new DateTime(1983, 5, 26) });
stuList.Add(new Student() { Name = "Zulia", Age = 31, City = "London", Dob = new DateTime(1981, 5, 26) });
stuList.Add(new Student() { Name = "Brandon", Age = 35, City = "London", Dob = new DateTime(1978, 5, 26) });

The record insertion should be done before the above binding. Run the project and the datagridview will be shown with these three records. The image shown the records in datagridview:

1 comment: