Home > DeveloperSection > Beginner > Ways to Bind DataGridView in Window Forms C#

Ways to Bind DataGridView in Window Forms C#


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 3786  View(s)
Rate this:

Ways to Bind DataGridView in Window Forms C#

 

Introduction

I would like to share multiple ways to bind datagridview in window forms using C#.

We will learn following ways to bind Datagridview.

a.       Binding DatagridView using Generic List

b.      Binding DatagridView using Datatable

c.       Binding DatagridView using Linq query result

d.      Binding DatagridView using Array

e.      Binding DatagridView using Two dimension Array

f.        Binding DatagridView Manually

 

  1. Binding with Generic List

1.                   Add following class to project

public class Emp

        {

            public int ID { getset; }

            public string Name { getset; }

            public string City { getset; }

 

            public Emp(int id, string name, string city)

            {

                this.ID = id;

                this.Name = name;

                this.City = city;

            }

        }

 

2.                   Create List of Emp

protected List<Emp> GetEmpList()

        {

            List<Emp> lEmp = new List<Emp>();

            Emp oemp = new Emp(1234, "Devesh Omar""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1234, "ROLI""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1235, "ROLI""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1236, "ROLI""Kanpur");

            lEmp.Add(oemp);

            oemp = new Emp(1237, "Manish Omar""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1238, "ROLI1""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1239, "ROLI2""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1230, "ROLI3""CNB");

            lEmp.Add(oemp);

            oemp = new Emp(1231, "ROLI4""CNB-UP");

            lEmp.Add(oemp);

            oemp = new Emp(1232, "ROLI5""GHAZIABAD");

            lEmp.Add(oemp);

            oemp = new Emp(1233, "ROLI6""UP");

            lEmp.Add(oemp);

            return lEmp;

 

 

        }

3.                   Binding Grid

dataGridView1.DataSource = GetEmpList();

Following would be screen.

Ways to Bind DataGridView in Window Forms C#

b.      Binding DatagridView using Datatable

Steps:

1.                   Create datatable and Define Columns

  DataTable table = new DataTable();

            table.Columns.Add("ID"typeof(int));

            table.Columns.Add("NAME"typeof(string));

            table.Columns.Add("CITY"typeof(string));

           

2.                   Add Rows

  table.Rows.Add(111, "Devesh""Ghaziabad");

            table.Rows.Add(222, "ROLI""KANPUR");

            table.Rows.Add(102, "ROLI""MAINPURI");

            table.Rows.Add(212, "DEVESH""KANPUR");

 

3.                   Binding Datagridview

dataGridView1.DataSource=table;

 

4.       Running the code following would be screen.

Ways to Bind DataGridView in Window Forms C#

 

c.        Binding DatagridView using Linq query result

5.                    First we need to create a Generic list , following would be sample code

protected List<Emp> GetEmpList()

              {

            List<Emp> lEmp = new List<Emp>();

            Emp oemp = new Emp(1234, "Devesh Omar""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1234, "ROLI""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1235, "ROLI""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1236, "ROLI""Kanpur");

            lEmp.Add(oemp);

            oemp = new Emp(1237, "Manish Omar""GZB");

            lEmp.Add(oemp);

            oemp = new Emp(1238, "ROLI1""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1239, "ROLI2""MainPuri");

            lEmp.Add(oemp);

            oemp = new Emp(1230, "ROLI3""CNB");

            lEmp.Add(oemp);

            oemp = new Emp(1231, "ROLI4""CNB-UP");

            lEmp.Add(oemp);

            oemp = new Emp(1232, "ROLI5""GHAZIABAD");

            lEmp.Add(oemp);

            oemp = new Emp(1233, "ROLI6""UP");

            lEmp.Add(oemp);

            return lEmp;

 

 

        }

6.                   Writing Linq query for above list

List<Emp> Lstemp = GetEmpList();

           var columns = from t in Lstemp

                          orderby t.Name

                          select new

                          {

                              EmpID = t.ID,

                              Name = t.Name,

                              City = t.City

                            

                          }; 

7.                   Binding Grid

dataGridView1.DataSource = columns.ToList();

 

8.                   Running the code.

Ways to Bind DataGridView in Window Forms C#

9.                   Adding Row_number to Linq query

List<Emp> Lstemp = GetEmpList();

            int Srno = 0;

            var columns = from t in Lstemp

                          orderby t.Name

                          select new

                          {

                              Row_number=++Srno,

                              EmpID = t.ID,

                              Name = t.Name,

                              City = t.City

                            

                          };

In this query we have Row_number=++Srno which would result into auto increment row and act as row number.

Ways to Bind DataGridView in Window Forms C#

d.       Binding DatagridView using Array

 

10.               Add following class to project

public class Emp

        {

            public int ID { getset; }

            public string Name { getset; }

            public string City { getset; }

 

            public Emp(int id, string name, string city)

            {

                this.ID = id;

                this.Name = name;

                this.City = city;

            }

        }

11.               Defining Array

var arrEmp = new[] {

                new Emp( 1, "Devesh Omar",  "Noida"),

                new Emp( 2, "Roli",  "Kanpur"),

                new Emp( 3, "Roli Gupta",  "Mainpuri"),

                 new Emp( 3, "Roli Gupta",  "Kanpur"),

                 new Emp( 3, "Devesh Roli ",  "Noida"),

            };

12.       Binding Grid

dataGridView1.DataSource = arrEmp;

 

e.      Binding DatagridView using Two dimension Array

13.                Declaring Array

string[][] Array = new string[100][];

14.               Adding values to array

for(int i = 0; i < 100; i++)

              Array[i] = new string[2] { "ROLI:"+i, "DEVESH:"+i };

 

15.               Defining Linq for array

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

16.               Binding data

dataGridView1.DataSource = data.ToList();

Ways to Bind DataGridView in Window Forms C#

 

f.        Binding DatagridView Manually

17.               Defining columns

           dataGridView1.ColumnCount = 3;

            dataGridView1.Columns[0].Name = "ID";

            dataGridView1.Columns[1].Name = "Name";

            dataGridView1.Columns[2].Name = "City";

18.               Adding Rows

string[] row = new string[] { "1""DEvesh omar""NOIDA" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "2""ROLI""KANPUR" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "3""DEVESH""NOIDA!22" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "4""ROLI""MAINPURI" };

            dataGridView1.Rows.Add(row);

 

19.               Running the code

Ways to Bind DataGridView in Window Forms C#

 

Conclusion

We have learn various ways to bind datagridview in c# window forms


Don't want to miss updates? Please click the below button!

Follow MindStick