Bind DropDownList based on another DropDownList in GridView Row
Html Source:
In this article I have explained,
how to bind the DropDownList in the GridView’s ItemTemplate. Sometime we need
to bind the DropDownList based on the other DropDownList SelectedItem in the row
of the GridView. Here I have populated the first DropDownList, based on its
SelectedItem I have popualted the second
DropDownList. Here I have used the DropDownList1’s SelectedIndexChanged event
to bind the second DropDownList.
Html Source:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RowID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Font-Size="Small" DataSource='<%#getvalues() %>' DataTextField="EventName"
DataValueField="EventID" Width="150px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Font-Size="Small" Width="150px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using Microsoft.ApplicationBlocks.Data;
public partial class Default4 :
System.Web.UI.Page
{
MainClass mc = new
MainClass();
public string
message;
public Boolean
active = false;
public long usertype;
public string con;
DataSet ds;
DataRow dr;
SqlCommand cmd = new
SqlCommand();
DataTable dtEmpty = new
DataTable();
DataTable dtGrid = new
DataTable();
public DataTable
dtnull = new DataTable();
public DataSet
dsCust = new DataSet();
protected void
Page_Load(object sender, EventArgs e)
{
mc.InitializeCulture();
if (!(Page.IsPostBack))
{
BindGrid();
}
}
public DataTable getvalues()
{
try
{
DataSet dsEvents = new
DataSet();
//Your method to fill the dataset from the DataBase
dsEvents = spcall(1);
ViewState["EventTable"] =
dsEvents;
return dsEvents.Tables[0];
}
catch (Exception
ex)
{
throw ex;
}
}
protected void
BindGrid()
{
try
{
DataTable dtab = dtSort();
DataRow d = dtEmpty.NewRow();
d["RowID"] = 0;
d["UserName"] = null;
d["Company_Name"] = null;
dtEmpty.Rows.Add(d);
GridView1.DataSource = dtEmpty;
GridView1.DataBind();
}
catch (Exception
ex)
{
throw ex;
}
}
private DataSet
spcall(long flag)
{
try
{
SqlParameter[] sql = new
SqlParameter[1];
sql[0] = new SqlParameter("@flag", SqlDbType.BigInt);
sql[0].Value = flag;
ds = SqlHelper.ExecuteDataset(mc.con,
CommandType.StoredProcedure, "SP_SelectEvent", sql);
return ds;
}
catch (Exception
ex)
{
throw ex;
}
}
private DataSet
spcallEvent(int _EventId)
{
try
{
SqlParameter[] sql = new
SqlParameter[1];
sql[0] = new SqlParameter("@EventID", SqlDbType.BigInt);
sql[0].Value = _EventId;
ds = SqlHelper.ExecuteDataset(mc.con,
CommandType.StoredProcedure, "SP_SelectCustName", sql);
return ds;
}
catch (Exception
ex)
{
throw ex;
}
}
public DataTable
dtSort()
{
DataColumn RowID = new
DataColumn("RowID");
dtEmpty.Columns.Add(RowID);
DataColumn colUname = new
DataColumn("UserName");
dtEmpty.Columns.Add(colUname);
DataColumn colcmp = new
DataColumn("Company_Name");
dtEmpty.Columns.Add(colcmp);
return
dtEmpty;
}
protected void
DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
// Here you can find the row which row's dropdown is fired
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
// Now convert sender as dropdown which is fired and you
can get all the properties of the dropdown
DropDownList ddl = (DropDownList)sender;
// Now fetch the data from the database based on selected
drodpwon and bind that data to another dropdown this way
DropDownList ddlsecond = gvr.FindControl("DropDownList2") as DropDownList;
// So in this you can get second drodpdown and bind your
data
dsCust = spcallEvent(Convert.ToInt32(
ddl.SelectedValue));
ddlsecond.DataSource = dsCust.Tables[0];
ddlsecond.DataTextField = "CustName";
ddlsecond.DataValueField = "ID";
ddlsecond.DataBind();
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.