In my previous tutorials I have bound the data using a datatable, but here we will use a dataset. It is easy to use but for some reason I thought that this will be beneficial for .NET beginners who have no idea about dataset and datatable.
Initial Chamber
Step 1
Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (gridview_demo).
Step 2
In Solution Explorer you get your empty website, then add a web form and a SQL Server database as in the following.
For Web Form:
gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it gridview_demo.aspx.
For SQL Server Database
gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder.)
DATABASE CHAMBER
Step 3
In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.
Table tbl_data (Don't Forget to make ID as IS Identity -- True)
Design Chamber
Step 4
Now open the gridview_demo.aspx file where we will create our design for binding a GridView using a dataset.
Gridview_demo.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <table style="width:100%;">
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
- CellPadding="4" DataKeyNames="id">
- <Columns>
- <asp:TemplateField HeaderText="Name">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="City">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("city") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
- <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
- <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
- <RowStyle BackColor="White" ForeColor="#003399" />
- <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
- <SortedAscendingCellStyle BackColor="#EDF6F6" />
- <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
- <SortedDescendingCellStyle BackColor="#D6DFDF" />
- <SortedDescendingHeaderStyle BackColor="#002876" />
- </asp:GridView>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- </table>
- <div>
- </div>
- </form>
- </body>
- </html>

Code Chamber
Step 5
Open the gridview_demo.aspx.cs file and write some code so that our application works.
Gridview_demo.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- public partial class Default2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- refreshdata();
- }
- }
- public void refreshdata()
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- }

I hope you liked it. Thank you for reading, Have a good day.

Niranjani KPosted Apr 21, 2021, 4:59 PM
I am getting sql exception can anyone help plz
Niranjani KPosted Apr 21, 2021, 4:57 PM
I am getting sql exception
Arpit UpadhyayPosted Mar 11, 2020, 2:58 AM
Nice Post. Thanks sir
Ricardo deVriesPosted Nov 4, 2018, 6:58 AM
Thank you! nice post
Firoz KhanPosted Nov 10, 2017, 4:46 PM
Http://www.aspdotnetkhan.com/bind-gridview-in-Asp-Net-C-Sharp-using-databse-49.aspx
Rahul SainiPosted Sep 16, 2017, 8:15 AM
Ossssssmmmmm artical
Arul RPosted Oct 28, 2015, 5:36 AM
Good one !!!
Rahul PrajapatPosted Aug 6, 2015, 7:58 AM
Nice article
Vipan SharmaPosted Aug 6, 2015, 12:29 AM
nice artical
Dan CerPosted Aug 5, 2015, 10:40 PM
I did somwthing similar here. https://m.youtube.com/watch?list=PL4TpT7opAQlL6moDuXjOs5SuFW8TgSpDp&v=8zUVs0aKlEQ
Dan CerPosted Aug 5, 2015, 10:39 PM
Nice tutorial, I did something herehttps://m.youtube.com/watch?list=PL4TpT7opAQlL6moDuXjOs5SuFW8TgSpDp&v=8zUVs0aKlEQ
Neeraj KumarPosted Aug 5, 2015, 5:44 PM
Nice Article
Sibeesh VenuPosted Aug 5, 2015, 6:31 AM
Nice Share
Raja TPosted Aug 5, 2015, 1:09 AM
Very useful for beginners..very nice
Debasis SahaPosted Aug 5, 2015, 1:02 AM
Nice one..
Upendra Pratap ShahiPosted Aug 5, 2015, 12:52 AM
nice sir
Chervine BhiwooPosted Aug 4, 2015, 11:19 PM
Good Article.
Santhakumar MunuswamyPosted Aug 4, 2015, 10:04 PM
Good Article. Thanks for sharing :)
Rahul PrajapatPosted Aug 4, 2015, 8:38 PM
Nice sir
RakeshPosted Aug 4, 2015, 2:07 PM
Good one
Manas MohapatraPosted Aug 4, 2015, 9:56 AM
Nice one Nilesh. Very useful for beginners..
Rajeesh MenothPosted Aug 4, 2015, 8:35 AM
Good One
Mohammed IbrahimPosted Aug 4, 2015, 7:55 AM
nice