Bind DataGridView in Window Forms Using C#

We use one GridView and a database. We enter some data into the database and then bind that data to a GridView.

Initial chamber

Step 1

Open your Visual Studio 2010 then go to File -> New -> Project then under Visual C# select the Windows node.

new project

You can change the name of the project and you can browse to create your project in a different location too. And then press OK.

Step 2

In Solution Explorer you get your project, then add a Service-Based database by going to your project then right-click and Add New Item then select Service Based database.

Identity Specification

Database chamber

Step 3

Get to your database (Database.mdf). We will create a table (tbl_Data). Go to the database.mdf then select Table then select Add New table. Design your table like this:

Table -> tbl_data (Don't forget to set ID -> Identity Specification -> Yes).

table

Design chamber

Step 4

Now open your Form1.cs file where we created our design for GridView binding.

We will drag a DataGridView from the toolbox to Form1.cs. You will see your form look like the following.

add field

You will see your GridView is empty when you dragged it to the form, we must add these headers for ID, Name and City By Going like this.

Right-click on the small arrow on the GridView then select Edit Column. The Edit Column window will open, on the bottom-left there is an “ADD” button, click on that. You will see an Add Colum window will open like this.

There you must add a field as we do for a bound field in ASP.NET. Here I had done it for Id. You must only do the same for name and city.

add column

After adding all that you will see something like these images.

Code

Here in the Bound Column Property there is a field, Data Property Name. It is the same as a Data Field in ASP.Net, so you must do something with Data Property Name, like for Id I had Included Id in my database, then for Name, name; for City, city. So fill that out and press OK.

Then you will see your GridView completed by header text. Now we must bind that.

Code chamber

Right-click on the blank part of Form1.cs then select View Code. You will see you are shown the code part of the form. Write the following code and then press F5 to run the project.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.             refreshdata();  
  19.         }  
  20.   
  21.             private void refreshdata()  
  22.             {  
  23.               
  24.                 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  25.                 SqlCommand cmd = new SqlCommand("select * from tbl_data",con);  
  26.                 con.Open();  
  27.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);             
  28.                 DataTable dt = new DataTable();  
  29.                 sda.Fill(dt);  
  30.                 con.Close();  
  31.                 dataGridView1.DataSource=dt;  
  32.             }  
  33.               
  34.               
  35.             }  
  36.         }  
Output chamber

Output chamber

I hope you like this. Thank you for reading. Have a good day.