Click here to Skip to main content
14,830,755 members
Articles / Web Development / ASP.NET
Posted 26 May 2006


55 bookmarked

How to populate DataGridView, GridView with SQL statement in C#

Rate me:
Please Sign up or sign in to vote.
2.97/5 (43 votes)
26 May 20062 min read
How to populate DataGridView, GridView with SQL statement in C#



How to populate DataGridView, GridView with SQL statement in C#<o:p>


When we do the development, we always want to make the code simple and error free if possible. In C#, GridView for web based application and DataGridView for windows form based application are different in using and behavior. It looks like Microsoft has two different teams to develop GridView and DataGridView separately.  This is why I wrote this article to share the coding for each control. Here I am using MS Visual Studio 2005.<o:p>


I. Populate GridView control for web based application with SQL statement


Let us put GridView1 control on the web form from Toolbox. The coding is straight forward and is like the following:<o:p>


protected void Page_Load(object sender, EventArgs e)<o:p>



string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";<o:p>

SqlConnection sqlConnection = new SqlConnection(strSQLconnection);<o:p>

SqlCommand sqlCommand = new SqlCommand("select * from table1", sqlConnection);<o:p>



SqlDataReader reader = sqlCommand.ExecuteReader();<o:p>


GridView1.DataSource = reader;<o:p>




You run the code and you can see the result. But when you see the data binding for DataGridView in the following section, it is quite different.



II. Populate DataGridView control with SQL statement for Window form based application<o:p>


When I used the DataGridView control in C# in MS Visual Studio 2005, I found DataGridView control is not friendly to use. Windows form-based DataGridView control is different from web based GridView control. DataGridView doesn’t have DataBind() method.  It took me a few days to figure out.

The logic is like this

  1. Create data set from SQL statement or stored procedure
  2. Create a table to hold this data set
  3. Create a BindingSource and bind this table with this BindingSource
  4. Then bind this BindingSource with GridView control.


This looks trivial. But I found it is very efficient and error free.


Let us put DataGridView control and BindingSource control on the windows form from Toolbox. Let us name DataGridView control as dbGridView, BindingSource control as dbBindSource. Let us apply the following code:


private void Form1_Load(object sender, EventArgs e)


string strCon = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";

string strSQL = “select * from table1”;


SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);

SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);


// Populate a new data table and bind it to the BindingSource.

DataTable table = new DataTable();

table.Locale = System.Globalization.CultureInfo.InvariantCulture;


dbBindSource.DataSource = table;


// Resize the DataGridView columns to fit the newly loaded content.


// you can make it grid readonly.

dbGridView.ReadOnly = true; 

// finally bind the data to the grid

dbGridView.DataSource = dbBindSource;



Now we compile it and run it. You can see the data in the grid.


If you have any comments or questions, you can reach me at


Enjoy C# and happy coding!

- Hongwei Li





This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author
Software Developer (Senior)
United States United States
C# and .NET Core
Typescript, node.js, jQuery, jQuery Mobile
Web Api and MVC
Angular and AngularJS
Databases (SQL Server and Oracle) and No-SQL Databases
Agile and Waterfall

Comments and Discussions

Questionhow populate data in grid. Pin
Member 112206289-Nov-14 22:06
MemberMember 112206289-Nov-14 22:06 
Questionwindows forms using c# Pin
Member 1110637814-Oct-14 21:09
MemberMember 1110637814-Oct-14 21:09 
Questionthumbs up!! Pin
Member 1005266429-Aug-13 9:01
MemberMember 1005266429-Aug-13 9:01 
GeneralMy vote of 5 Pin
cpt_Steel20-May-12 2:09
Membercpt_Steel20-May-12 2:09 
QuestionChild list for field " "cannot be created. Pin
Tooze15-May-12 7:45
MemberTooze15-May-12 7:45 
GeneralMy vote of 5 Pin
horaceleung3-May-12 5:42
Memberhoraceleung3-May-12 5:42 
GeneralMy vote of 4 Pin
Itz.Irshad28-Dec-11 3:41
MemberItz.Irshad28-Dec-11 3:41 
GeneralMy vote of 5 Pin
keshavkundal19-Nov-10 23:02
Memberkeshavkundal19-Nov-10 23:02 
GeneralMy vote of 4 Pin
Member 263644025-Oct-10 1:38
MemberMember 263644025-Oct-10 1:38 
GeneralMy vote of 2 Pin
KSagar20-Aug-10 2:04
MemberKSagar20-Aug-10 2:04 
GeneralMy vote of 1 Pin
blackr2d5-Jul-10 23:05
Memberblackr2d5-Jul-10 23:05 
GeneralMy vote of 1 Pin
adolfhardik11-Jun-10 0:40
Memberadolfhardik11-Jun-10 0:40 
Generalmy vote of 2 Pin
mbyamukama18-Sep-09 20:56
Membermbyamukama18-Sep-09 20:56 
GeneralMy vote of 1 Pin
demigod231-May-09 3:59
Memberdemigod231-May-09 3:59 
GeneralRe: My vote of 1 Pin
supermankelly6-Aug-09 2:56
Membersupermankelly6-Aug-09 2:56 
GeneralMy vote of 1 Pin
Utku KAYA18-Jan-09 23:20
MemberUtku KAYA18-Jan-09 23:20 
GeneralMy vote of 2 Pin
Donsw26-Nov-08 16:40
MemberDonsw26-Nov-08 16:40 
GeneralRe: My vote of 2 Pin
supermankelly6-Aug-09 2:54
Membersupermankelly6-Aug-09 2:54 
GeneralRegurgitated Crap Pin
pithhelmet4-Sep-08 4:44
Memberpithhelmet4-Sep-08 4:44 
GeneralRe: Regurgitated Crap Pin
supermankelly6-Aug-09 2:53
Membersupermankelly6-Aug-09 2:53 
Generaltry to solve Pin
Nazneen Zahid28-Aug-08 20:38
MemberNazneen Zahid28-Aug-08 20:38 
Questionnice copy paste? Pin
Laoujin23-Apr-08 22:35
MemberLaoujin23-Apr-08 22:35 
GeneralFilling DataGridView using Stored Procedures Pin
buyValu13-Feb-08 8:34
MemberbuyValu13-Feb-08 8:34 
GeneralRe: Filling DataGridView using Stored Procedures Pin
Glen Austin31-Mar-09 12:20
MemberGlen Austin31-Mar-09 12:20 
GeneralThanks! Exactly what I was looking for. Pin
tmpuserz27-Nov-07 10:16
Membertmpuserz27-Nov-07 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.