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)

Table

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:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <table style="width:100%;">
  10. <tr>
  11. <td>
  12. </td>
  13. <td>
  14. </td>
  15. <td>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>
  20. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  21. BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
  22. CellPadding="4" DataKeyNames="id">
  23. <Columns>
  24. <asp:TemplateField HeaderText="Name">
  25. <EditItemTemplate>
  26. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
  27. </EditItemTemplate>
  28. <ItemTemplate>
  29. <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
  30. </ItemTemplate>
  31. </asp:TemplateField>
  32. <asp:TemplateField HeaderText="City">
  33. <EditItemTemplate>
  34. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
  35. </EditItemTemplate>
  36. <ItemTemplate>
  37. <asp:Label ID="Label2" runat="server" Text='<%# Bind("city") %>'></asp:Label>
  38. </ItemTemplate>
  39. </asp:TemplateField>
  40. </Columns>
  41. <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  42. <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
  43. <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  44. <RowStyle BackColor="White" ForeColor="#003399" />
  45. <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  46. <SortedAscendingCellStyle BackColor="#EDF6F6" />
  47. <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
  48. <SortedDescendingCellStyle BackColor="#D6DFDF" />
  49. <SortedDescendingHeaderStyle BackColor="#002876" />
  50. </asp:GridView>
  51. </td>
  52. <td>
  53. </td>
  54. <td>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td>
  59. </td>
  60. <td>
  61. </td>
  62. <td>
  63. </td>
  64. </tr>
  65. </table>
  66. <div>
  67. </div>
  68. </form>
  69. </body>
  70. </html>
Your design will look like the following:

Design

Code Chamber

Step 5

Open the gridview_demo.aspx.cs file and write some code so that our application works.

Gridview_demo.cs:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class Default2 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!Page.IsPostBack)
  14. {
  15. refreshdata();
  16. }
  17. }
  18. public void refreshdata()
  19. {
  20. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  21. SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
  22. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  23. DataSet ds = new DataSet();
  24. sda.Fill(ds);
  25. GridView1.DataSource = ds;
  26. GridView1.DataBind();
  27. }
  28. }
Output Chamber

GridView

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