Initial chamber

Step 1: Open Visual Studio 2010 and create an empty Website. Give a suitable name [gridview_demo].

Step 2: In Solution Explorer you will get your empty website, Add a web form, SQL Database. Here are the steps:

For Web Form:

gridview_demo (Your Empty Website) - Right Click, Add New Item, Web Form. Name it gridview_demo.aspx.

For SQL Server Database:

gridview_demo (Your Empty Website) - Right Click, Add New Item, SQL Server Database. [Add Database inside the App_Data_folder].

Database chamber

Step 3: Get to your Database [Database.mdf], we will create two table, then tbl_employee, Go to the database.mdf, Table, Add New table, design your table like this:

Table - tbl_employee [Don’t forget to make ID, Identity Specification - Yes]

Tbl_employee:

table design

Tbl_qualification:

table

Design chamber

Step 4: Now open your gridview_demo.aspx file, where we create our design so that our application works.

gridview_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  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. <div>
  10. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  11. BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
  12. CellPadding="3" DataKeyNames="id" GridLines="Vertical"
  13. onrowdatabound="GridView1_RowDataBound">
  14. <AlternatingRowStyle BackColor="#DCDCDC" />
  15. <Columns>
  16. <asp:TemplateField HeaderText="Employee ID">
  17. <EditItemTemplate>
  18. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
  19. </EditItemTemplate>
  20. <ItemTemplate>
  21. <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
  22. </ItemTemplate>
  23. </asp:TemplateField>
  24. <asp:TemplateField HeaderText="Emloyee Name">
  25. <EditItemTemplate>
  26. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
  27. </EditItemTemplate>
  28. <ItemTemplate>
  29. <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
  30. </ItemTemplate>
  31. </asp:TemplateField>
  32. <asp:TemplateField HeaderText="Qualification">
  33. <EditItemTemplate>
  34. <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  35. </EditItemTemplate>
  36. <ItemTemplate>
  37. <asp:DropDownList ID="DropDownList1" runat="server">
  38. </asp:DropDownList>
  39. </ItemTemplate>
  40. </asp:TemplateField>
  41. <asp:TemplateField HeaderText="Employee City">
  42. <EditItemTemplate>
  43. <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
  44. </EditItemTemplate>
  45. <ItemTemplate>
  46. <asp:Label ID="Label4" runat="server" Text='<%# Bind("city") %>'></asp:Label>
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. </Columns>
  50. <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
  51. <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  52. <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  53. <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  54. <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  55. <SortedAscendingCellStyle BackColor="#F1F1F1" />
  56. <SortedAscendingHeaderStyle BackColor="#0000A9" />
  57. <SortedDescendingCellStyle BackColor="#CAC9C9" />
  58. <SortedDescendingHeaderStyle BackColor="#000065" />
  59. </asp:GridView>
  60. </div>
  61. </form>
  62. </body>
  63. </html>
Your design looks like the following image:

design

Code chamber

Step 5: Open gridview_demo.aspx.cs and write some code so that our application works.
  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 _Default : System.Web.UI.Page
  10. {
  11. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!Page.IsPostBack)
  15. {
  16. refreshdata();
  17. }
  18. }
  19. public void refreshdata()
  20. {
  21. con.Open();
  22. SqlCommand cmd = new SqlCommand("select * from tbl_employee", con);
  23. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  24. DataTable dt = new DataTable();
  25. sda.Fill(dt);
  26. con.Close();
  27. GridView1.DataSource = dt;
  28. GridView1.DataBind();
  29. }
  30. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  31. {
  32. if (e.Row.RowType == DataControlRowType.DataRow)
  33. {
  34. con.Open();
  35. DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
  36. SqlCommand cmd = new SqlCommand("select * from tbl_qualification", con);
  37. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  38. DataTable dt = new DataTable();
  39. sda.Fill(dt);
  40. con.Close();
  41. DropDownList1.DataSource = dt;
  42. DropDownList1.DataTextField = "qualification";
  43. DropDownList1.DataValueField = "qualification";
  44. DropDownList1.DataBind();
  45. DropDownList1.Items.Insert(0, new ListItem("--Select Qualification--", "0"));
  46. }
  47. }
  48. }
Output chamber

Output

dropdown list

Hope you liked this. Thank you for reading. Have a good day.