C#.Net GridView Example – How To: Bind Gridview in Asp.net with SQLDataSource in Code behind

In one of previous article we have seen how to bind the Gridview with SQLDataSource at Design time. In that article we also explained what is GridView and why we need it?However, sometimes it is needed to bind DataSource with GridView at runtime in code behind. In this article let’s learn how to bind database with GridView in Code behind. Once the SQLDataSource is bound, you will be able to perform insert, update, delete in Gridview.

So in this article let’s take an example of “NorthWind” database which comes with SQL Server 2005. We will bind “Employee” table from “NorthWind” database to “SqlDataSource” and then will bind that “SqlDataSource” to GridView in asp.net. Please note that “NorthWind” database doesn’t come with SQL Server 2008. Hence, if you are using SQL server 2008 then please download “NorthWind” database backup from Microsoft’s link and restore it in your machine.

How to bind GridView in asp.net with SQLDataSource in code behind?

  1. Create a new Asp.net Web application.
  2. Add GridView in asp.net page. To add asp.net GridView in your page, drag & drop gridview control from toolbox as shown below –

    GridView control in toolbox
    GridView control in toolbox
  3. As soon as you drag & drop GridView in Asp.net, it would look like below –

    GridView on web page after drag & drop
    GridView on web page after drag & drop

    Observe that the Gridview is showing column name as “Column0”, “Column1” and “Column2”. The reason is DataSource not yet bind to GridView.

  4. Now go to aspx.cs page where you added the GridView. In this example I have added GridView in “Default.aspx” hence I have opened “Default.aspx.cs” page.
  5. Now write below code in Page_Load Event –
       protected voidPage_Load(object sender, EventArgs e)
           // ConnectionString to NorthWind Database.
           string connectionString = “Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True”;
           // Create SQLDataSource.
           SqlDataSource sqlDataSource = new SqlDataSource();
           sqlDataSource.ID = “SqlDataSource123”;
           // Bind ConnectionString to SQLDataSource.
           sqlDataSource.ConnectionString = connectionString;
           // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
           sqlDataSource.SelectCommand = “SELECT EmployeeID, LastName, FirstName, Title, BirthDate  FROM Employees”;
           // Bind SQLDataSource to GridView after retrieving the records.
           GridView1.DataSource = sqlDataSource;

    That’s it. You are done with the code part. Save the file.

  6. Now execute the Web Application and see if the records from NorthWind database are populating on your C# gridview. Final output would like below screen –

    GridView Showing records bind in Code Behind
    GridView – SQLDataSource bind in Code Behind

Now look at the format of the Birthdate column. The birthdates are showing in default datetime format of GridView. You can change the format of datetime column. Please refer previous post on how to change the format of DateTime column in GridView.

From this gridview example in asp.net we have learnt to bind gridview in asp.net with SQLdatasource at code behind. Here is another article that shows how to bind the Gridview with SQLDataSource at Design time.

I hope this gridview example in asp.net helps you to configure the GridView with SQLDataSource in code behind. If you find this article helpful, then could you please share the article on your social network?

Following articles might be of your interest

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

5 Comments+ Add Comment

  • Nice Article Easy to understand Thank You

  • superb information i had used it on my site and it is sucessfully executed…thnkss….!!!

  • Nice thank you

  • Nice. It’s help lot..

  • Good one … Thanks a lot.

Leave a comment