Ways to Bind DataGridView in C# Windows Forms

Introduction

I would like to share multiple ways to bind a DataGridView to a Window Forms form using C#.

We will learn the following ways to bind a DataGridView.

  1. Binding DataGridView using Generic List
  2. Binding DataGridView using DataTable
  3. Binding DataGridView using LINQ query result
  4. Binding DataGridView using Array
  5. Binding DataGridView using a two-dimension array
  6. Binding DataGridView manually

1. Binding with Generic List

Add the following class to the project:

  1. public class Emp  
  2. {  
  3.     public int ID { getset; }  
  4.     public string Name { getset; }  
  5.     public string City { getset; }   
  6.     public Emp(int id, string name, string city)  
  7.     {  
  8.         this.ID = id;  
  9.         this.Name = name;  
  10.         this.City = city;  
  11.     }  

Create List of Emp

  1. protected List<Emp> GetEmpList()  
  2. {  
  3.     List<Emp> lEmp = new List<Emp>();  
  4.     Emp oemp = new Emp(1234, "Devesh Omar""GZB");  
  5.     lEmp.Add(oemp);  
  6.     oemp = new Emp(1234, "ROLI""GZB");  
  7.     lEmp.Add(oemp);  
  8.     oemp = new Emp(1235, "ROLI""MainPuri");  
  9.     lEmp.Add(oemp);  
  10.     oemp = new Emp(1236, "ROLI""Kanpur");  
  11.     lEmp.Add(oemp);  
  12.     oemp = new Emp(1237, "Manish Omar""GZB");  
  13.     lEmp.Add(oemp);  
  14.     oemp = new Emp(1238, "ROLI1""MainPuri");  
  15.     lEmp.Add(oemp);  
  16.     oemp = new Emp(1239, "ROLI2""MainPuri");  
  17.     lEmp.Add(oemp);  
  18.     oemp = new Emp(1230, "ROLI3""CNB");  
  19.    lEmp.Add(oemp);  
  20.    oemp = new Emp(1231, "ROLI4""CNB-UP");  
  21.    lEmp.Add(oemp);  
  22.    oemp = new Emp(1232, "ROLI5""GHAZIABAD");  
  23.    lEmp.Add(oemp);  
  24.    oemp = new Emp(1233, "ROLI6""UP");  
  25.    lEmp.Add(oemp);  
  26.    return lEmp;  

Binding Grid

  1. dataGridView1.DataSource = GetEmpList(); 

The following will be the screen.

Binding with Generic List

2. Binding DataGridView using DataTable

Steps

Create a DataTable and define the columns as in the following:
  1. DataTable table = new DataTable();  
  2. table.Columns.Add("ID"typeof(int));  
  3. table.Columns.Add("NAME"typeof(string));  
  4. table.Columns.Add("CITY"typeof(string)); 

Add Rows

  1. table.Rows.Add(111, "Devesh""Ghaziabad");  
  2. table.Rows.Add(222, "ROLI""KANPUR");  
  3. table.Rows.Add(102, "ROLI""MAINPURI");  
  4. table.Rows.Add(212, "DEVESH""KANPUR"); 

Binding DataGridView

  1. dataGridView1.DataSource=table; 

Running the code, the following will be the screen.

Binding DatagridView using Datatable

3. Binding DataGridView using LINQ query result

First we need to create a Generic list, the following is the sample code:

  1. protected List<Emp> GetEmpList()  
  2. {  
  3.     List<Emp> lEmp = new List<Emp>();  
  4.     Emp oemp = new Emp(1234, "Devesh Omar""GZB");  
  5.     lEmp.Add(oemp);  
  6.     oemp = new Emp(1234, "ROLI""GZB");  
  7.     lEmp.Add(oemp);  
  8.     oemp = new Emp(1235, "ROLI""MainPuri");  
  9.     lEmp.Add(oemp);  
  10.     oemp = new Emp(1236, "ROLI""Kanpur");  
  11.     lEmp.Add(oemp);  
  12.     oemp = new Emp(1237, "Manish Omar""GZB");  
  13.     lEmp.Add(oemp);  
  14.     oemp = new Emp(1238, "ROLI1""MainPuri");  
  15.     lEmp.Add(oemp);  
  16.     oemp = new Emp(1239, "ROLI2""MainPuri");  
  17.     lEmp.Add(oemp);  
  18.     oemp = new Emp(1230, "ROLI3""CNB");  
  19.     lEmp.Add(oemp);  
  20.     oemp = new Emp(1231, "ROLI4""CNB-UP");  
  21.     lEmp.Add(oemp);  
  22.     oemp = new Emp(1232, "ROLI5""GHAZIABAD");  
  23.     lEmp.Add(oemp);  
  24.     oemp = new Emp(1233, "ROLI6""UP");  
  25.     lEmp.Add(oemp);  
  26.     return lEmp;  

Writing a LINQ query for the list above:

  1. List<Emp> Lstemp = GetEmpList();  
  2. var columns = from t in Lstemp  
  3. orderby t.Name  
  4. select new  
  5. {  
  6.     EmpID = t.ID,  
  7.     Name = t.Name,  
  8.     City = t.City  
  9. };  

Binding Grid

  1. dataGridView1.DataSource = columns.ToList(); 

Running the code.

Binding DatagridView using Linq

Adding Row_number to the LINQ query:
  1. List<Emp> Lstemp = GetEmpList();  
  2. int Srno = 0;  
  3. var columns = from t in Lstemp  
  4. orderby t.Name  
  5. select new  
  6. {  
  7.     Row_number=++Srno,  
  8.     EmpID = t.ID,  
  9.     Name = t.Name,  
  10.     City = t.City  
  11. }; 

In this query we have Row_number=++Srno that would result in an auto-increment row and act as a row number.

query

4. Binding DataGridView using Array

Add the following class to the project:
  1. public class Emp  
  2. {  
  3.     public int ID { getset; }  
  4.     public string Name { getset; }  
  5.     public string City { getset; }  
  6.   
  7.     public Emp(int id, string name, string city)  
  8.     {  
  9.        this.ID = id;  
  10.        this.Name = name;  
  11.        this.City = city;  
  12.     }  

Defining Array

  1. var arrEmp = new[] {  
  2.     new Emp( 1, "Devesh Omar""Noida"),  
  3.     new Emp( 2, "Roli""Kanpur"),  
  4.     new Emp( 3, "Roli Gupta""Mainpuri"),  
  5.     new Emp( 3, "Roli Gupta""Kanpur"),  
  6.     new Emp( 3, "Devesh Roli ""Noida"),  
  7. }; 

Binding Grid

  1. dataGridView1.DataSource = arrEmp; 

Binding DataGridView using Array 

5. Binding DataGridView using Two dimension Array

Declaring Array
  1. string[][] Array = new string[100][]; 

Adding values to the array:

  1. for(int i = 0; i < 100; i++)  
  2. Array[i] = new string[2] { "ROLI:"+i, "DEVESH:"+i }; 

Defining LINQ for array:

  1. var data = (from arr in Array select new { Column_1 = arr[0], Column_2 = arr[1] }); 

Binding data

  1. dataGridView1.DataSource = data.ToList(); 

Binding DatagridView using Two dimension Array 

6. Binding DataGridView Manually

Defining columns
  1. dataGridView1.ColumnCount = 3;  
  2. dataGridView1.Columns[0].Name = "ID";  
  3. dataGridView1.Columns[1].Name = "Name";  
  4. dataGridView1.Columns[2].Name = "City"

Adding Rows

  1. string[] row = new string[] { "1""DEvesh omar""NOIDA" };  
  2. dataGridView1.Rows.Add(row);  
  3. row = new string[] { "2""ROLI""KANPUR" };  
  4. dataGridView1.Rows.Add(row);  
  5. row = new string[] { "3""DEVESH""NOIDA!22" };  
  6. dataGridView1.Rows.Add(row);  
  7. row = new string[] { "4""ROLI""MAINPURI" };  
  8. dataGridView1.Rows.Add(row); 

Running the code:

Binding DatagridView

Conclusion

We have learned various ways to bind a DataGridView to a C# Windows Forms form.

Up Next
    Ebook Download
    View all
    Learn
    View all