I am writing this article because I got a request from one of my users and friends. He stuck in this business requirement and asked me to write something about this. So I will show this functionality in 2 ways. Here in this part I will show this using ASP.NET C# and SQL Server and in the next part I will show it using jQuery.

Figure 1 shows my Data Table in design mode from which I will show this requirement.

table design
Figure 1

Script of My Table

  1. CREATE TABLE [dbo].[EmployeeTeam](
  2. [Employee_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Manager_ID] [int] NULL,
  5. [Email] [varchar](50) NULL,
  6. [Mobile] [varchar](50) NULL,
  7. [Country] [varchar](50) NULL,
  8. [IsManager] [bit] NULL,
  9. CONSTRAINT [PK_EmployeeTeam] PRIMARY KEY CLUSTERED
  10. (
  11. [Employee_ID] ASC
  12. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  13. ) ON [PRIMARY]
  14. GO
  15. SET ANSI_PADDING OFF
  16. GO

Figure 2 shows the data in the table.

table
Figure 2.

Here In this you can see I have employee records with Manager Id. So in DropDown I will see only Manager and on selecting a Manager from the DropDown I will show the team information in the GridView:

The following is my aspx:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DropDownGridView.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Fill Grid View On Selecting Record From Drop Down</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <table style="width: 100%; text-align: center; border: solid 5px red; background-color: yellow; vertical-align: top;">
  10. <tr>
  11. <td>
  12. <div>
  13. <fieldset style="width: 99%;">
  14. <legend style="font-size: 20pt; color: red; font-family: Verdana">Fill Grid View On Selecting Record From Drop Down</legend>
  15. <table style="padding: 20px; background-color: skyblue; width: 100%; text-align: center;">
  16. <tr style="background-color: orange; height: 40px;">
  17. <td style="width: 25%; text-align: left; padding-left: 20px; font-family: Verdana">Select Manager: </td>
  18. <td style="text-align: left;">
  19. <asp:DropDownList ID="ddlManager" runat="server" AutoPostBack="True"
  20. OnSelectedIndexChanged="ddlManager_SelectedIndexChanged" Height="18px"
  21. Width="200px" CausesValidation="True">
  22. </asp:DropDownList><br />
  23. </td>
  24. </tr>
  25. <tr>
  26. <td></td>
  27. </tr>
  28. <tr>
  29. <td colspan="2">
  30. <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="100%"
  31. BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" Font-Names="verdana"
  32. CellPadding="4" GridLines="Horizontal" EmptyDataText="There is no Employee.">
  33. <Columns>
  34. <asp:BoundField HeaderText="Emp Id" DataField="Employee_Id" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
  35. <asp:BoundField HeaderText="Emp Name" DataField="Name" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
  36. <asp:BoundField HeaderText="Email" DataField="Email" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
  37. <asp:BoundField HeaderText="Mobile" DataField="Mobile" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
  38. <asp:BoundField HeaderText="Country" DataField="Country" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
  39. </Columns>
  40. <FooterStyle BackColor="White" ForeColor="#333333" />
  41. <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
  42. <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
  43. <RowStyle BackColor="White" ForeColor="#333333" />
  44. <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
  45. <SortedAscendingCellStyle BackColor="#F7F7F7" />
  46. <SortedAscendingHeaderStyle BackColor="#487575" />
  47. <SortedDescendingCellStyle BackColor="#E5E5E5" />
  48. <SortedDescendingHeaderStyle BackColor="#275353" />
  49. </asp:GridView>
  50. </td>
  51. </tr>
  52. <tr>
  53. <td colspan="2"></td>
  54. </tr>
  55. </table>
  56. </fieldset>
  57. </div>
  58. </td>
  59. </tr>
  60. </table>
  61. </form>
  62. </body>
  63. </html>

Now my aspx.cs code is:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. namespace DropDownGridView
  11. {
  12. public partial class Default : System.Web.UI.Page
  13. {
  14. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EMPCON"].ConnectionString);
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. if (!Page.IsPostBack)
  18. {
  19. BindAllManagerDropDown();
  20. }
  21. }
  22. protected void BindAllManagerDropDown()
  23. {
  24. SqlCommand cmd = new SqlCommand();
  25. SqlDataAdapter da = new SqlDataAdapter();
  26. DataTable dt = new DataTable();
  27. try
  28. {
  29. cmd = new SqlCommand("Select * from EmployeeTeam WHERE IsManager=1", con);
  30. da.SelectCommand = cmd;
  31. da.Fill(dt);
  32. ddlManager.DataSource = dt;
  33. ddlManager.DataTextField = "Name";
  34. ddlManager.DataValueField = "Employee_Id";
  35. ddlManager.DataBind();
  36. ddlManager.Items.Insert(0, "-- Select Manager --");
  37. }
  38. catch (Exception ex)
  39. {
  40. }
  41. finally
  42. {
  43. cmd.Dispose();
  44. da.Dispose();
  45. dt.Clear();
  46. dt.Dispose();
  47. }
  48. }
  49. protected void ddlManager_SelectedIndexChanged(object sender, EventArgs e)
  50. {
  51. try
  52. {
  53. int managerID = Convert.ToInt32(ddlManager.SelectedValue);
  54. BindManagerEMPLOYEE(managerID);
  55. }
  56. catch (Exception ex)
  57. {
  58. }
  59. }
  60. private void BindManagerEMPLOYEE(int managerID)
  61. {
  62. DataTable dt = new DataTable();
  63. SqlDataAdapter adp = new SqlDataAdapter();
  64. try
  65. {
  66. SqlCommand cmd = new SqlCommand("select * from EmployeeTeam where Manager_ID=" + managerID + " ", con);
  67. adp.SelectCommand = cmd;
  68. adp.Fill(dt);
  69. if (dt.Rows.Count > 0)
  70. {
  71. GridViewEmployee.DataSource = dt;
  72. GridViewEmployee.DataBind();
  73. }
  74. else
  75. {
  76. GridViewEmployee.DataSource = null;
  77. GridViewEmployee.DataBind();
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. }
  83. finally
  84. {
  85. dt.Clear();
  86. dt.Dispose();
  87. adp.Dispose();
  88. }
  89. }
  90. }
  91. }

The following is the the connection string in the Web.config file:

  1. <connectionStrings>
  2. <add name="EMPCON" connectionString="Data Source=INDIA\MSSQLServer2k8;Initial Catalog=TestDB;Integrated Security=True"/>
  3. </connectionStrings>

connectionStrings
Figure 3

Figures 4 - 9 show the resutls from executing the application.

run your application
Figure 4

select manager name
Figure 5

emp id
Figure 6

select manager
Figure 7

select record
Figure 8

fill gridview
Figure 9

In the next article I will show this functionality using jQuery.