Overview
Today, we will see how we can implement cascading dropdown grid view in C#. Before starting this article, let's have a fair amount of knowledge of a grid view control; i.e., how to bind grid view.
Introduction
- Open Visual Studio -> Create New Project, as shown below:
- Create a new Application and name it TestCascade,
- We will be including bootstrap themes. Kindly download bootstrap from manage Nugets or you can download from the official website.
- Kindly implement the styles which we have downloaded from the website.
- Now, let's create two tables as Customer info and customerdetails.
- Insert some data in the two tables, in order to show some data in a grid view.
- After inserting, just see whether the data is entered properly or not and which fields are required to be shown.
- Let's start with the designing page,
- ASPX Page
- Default.aspx
- First add two images. Adjust the screen, using add and minus, to cascade the grid view.
- Create a grid view first and now refe to the following screenshot:
- Kindly add image. Add Image in <asp:Templatefield> to cascade dropdown of the grid view.
- Bind the fields of the first table Customerinfo.
- You have to bind another grid view.
- Our final code will be:
- Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TEstCascade._Default" %>
-
- <!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>
- <link type="text/css" href="Styles/bootstrap.css" rel="Stylesheet" />
- <link type="text/css" href="Styles/bootstrap.min.css" rel="Stylesheet" />
- <link type="text/css" href="Styles/MenuTableTemplate.css" rel="Stylesheet"/>
- <link type="text/css" href="Styles/sb-admin-2.css" rel="Stylesheet"/>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False"
- Width="100%" OnRowDataBound="gvParent_RowDataBound" OnRowCommand="gvParent_RowCommand"
- CssClass="table" Font-Size="11pt" Font-Names="Arial"
-
- >
-
- <Columns>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:ImageButton ID="btnImage" runat="server" ImageUrl="~/Images/add-button--thin-line--ios-7-interface-symbol_318-33624 (1).jpg" CommandName="expand" Height="20px" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="CUSTOMERID" HeaderText="Customer ID"></asp:BoundField>
- <asp:BoundField DataField="CUSTOMERNAME" HeaderText="Customer Name"></asp:BoundField>
- <asp:BoundField DataField="CUSTOMERADDRESS" HeaderText="Address"></asp:BoundField>
- <asp:BoundField DataField="CUSTOMERNUMBER" HeaderText="Number"></asp:BoundField>
- <asp:TemplateField>
- <ItemTemplate>
- </td></tr>
- <tr>
- <td>
- </td>
- <td colspan="4">
- <asp:GridView ID="gvChild" runat="server" Width="100%" Visible="False" AutoGenerateColumns="False"
- DataSource='<%# GetChildRelation(Container.DataItem,"Relation") %>'
- CssClass="table" Font-Size="11pt" Font-Names="Arial"
-
- >
-
- <Columns>
- <asp:BoundField DataField="CUSTOMERPANID" HeaderText="Pan ID"></asp:BoundField>
- <asp:BoundField DataField="CUSTOMERAADHARNO" HeaderText="Aadhar No"></asp:BoundField>
- <asp:BoundField DataField="CUSTOMERBALANCE" HeaderText="Balance"></asp:BoundField>
-
- </Columns>
-
-
- </asp:GridView>
- </td>
- </tr>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
-
-
- </asp:GridView>
- </form>
- </body>
- </html>
- CS Code
We need to combine both the grid views, and the grid views are parent and child. We need to bind the data of the tables of the parent and the child tables as customerinfo and customerdetails respectively .
- We need to specify the grid view command on the row command, as well.
- GETDATA() helps to map the selected query.
- SELECT CUSTOMERID,CUSTOMERNAME,CUSTOMERADDRESS,CUSTOMERNUMBER FROM CUSTOMERINFO;SELECT CUSTOMERID,CUSTOMERPANID,CUSTOMERAADHARNO,CUSTOMERBALANCE FROM CUSTOMERDETAIL
- Final CS Code
- 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;
-
- namespace TEstCascade
- {
- public partial class _Default : System.Web.UI.Page
- {
-
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- if (!IsPostBack)
- {
- DataSet ds = new DataSet();
- ds = GetData();
- ds.Tables[0].TableName = "Parent";
- ds.Tables[1].TableName = "Child";
- DataRelation dr = new DataRelation("Relation", ds.Tables["Parent"].Columns["CUSTOMERID"], ds.Tables["Child"].Columns["CUSTOMERID"], false);
- dr.Nested = true;
- ds.Relations.Add(dr);
- gvParent.DataSource = ds;
- gvParent.DataMember = "Parent";
- gvParent.DataBind();
- }
- }
-
- protected DataView GetChildRelation(object pDataItem, string pRelation)
- {
- DataRowView pvoDataRowView;
- pvoDataRowView = (DataRowView)pDataItem;
- if (pvoDataRowView != null)
- return pvoDataRowView.CreateChildView(pRelation);
- else
- return null;
- }
-
- protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "expand")
- {
- int pviIndex;
- pviIndex = Convert.ToInt16(e.CommandArgument.ToString());
- ImageButton btn = new ImageButton();
- btn = (ImageButton)gvParent.Rows[pviIndex].FindControl("btnImage");
- if (btn.ImageUrl.ToLower().Contains("add-button--thin-line--ios-7-interface-symbol_318-33624 (1).jpg"))
- {
- btn.ImageUrl = "Images/plainicon.com-43942-128px.png";
- ((GridView)gvParent.Rows[pviIndex].FindControl("gvChild")).Visible = true;
- }
- else
- {
- btn.ImageUrl = "Images/add-button--thin-line--ios-7-interface-symbol_318-33624 (1).jpg";
- ((GridView)gvParent.Rows[pviIndex].FindControl("gvChild")).Visible = false;
- }
- }
- }
-
- protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- ImageButton btn = new ImageButton();
- btn = (ImageButton)e.Row.Cells[0].FindControl("btnImage");
- btn.CommandArgument = e.Row.RowIndex.ToString();
- }
- }
-
-
- private DataSet GetData()
- {
- SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
- DataSet ds = new DataSet();
-
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandText = "SELECT CUSTOMERID,CUSTOMERNAME,CUSTOMERADDRESS,CUSTOMERNUMBER FROM CUSTOMERINFO;SELECT CUSTOMERID,CUSTOMERPANID,CUSTOMERAADHARNO,CUSTOMERBALANCE FROM CUSTOMERDETAIL";
- cmd.CommandType = CommandType.Text;
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(ds);
- con.Close();
- return ds;
- }
- public string ConnectionString { get; set; }
- }
- }
- Run the solution now.
- Let's check the dataset of the first table.
- Table no 2,
- Now, let's see the parent child table.
- The view of the parent table is as follows:
- The view of the child table is as follows:
- Now, let's see our final output.
- Click add image.
- Now, since you had cascaded the grid view, add the image you changed to the minus image button, and you can see the respective details as well.
- Now, click minus to check if everything works properly or not.
- The output shown below is perfect.
Conclusion
This was cascading dropdown grid view.