In this article I will explain with an example how to use ASP.Net DropDownList control in the EditItemTemplate of ASP.Net GridView control.

tbl_BalanceGroup

I created this GridView with EditItemTemplate.

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="CodeAction"
OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CodeAction" HeaderText="CodeAction" ReadOnly="True" SortExpression="CodeAction" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Group" SortExpression="GroupID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("GroupID") %>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlGroup" runat="server" DataTextField="Group" DataValueField="IDGroup"
AutoPostBack="True" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("GroupID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subgroup" SortExpression="SubGroupID">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("SubGroupID") %>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlSubgroup" runat="server" DataTextField="Group" DataValueField="IDGroup">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("SubGroupID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

The code behide is this:

        CnnString cnn = new CnnString();
public reportGroup rg = new reportGroup();

private void LoadData()
{
Schema.DataClassesDataContext pdc = new Schema.DataClassesDataContext(cnn.getString());
Schema.tbl_BalanceGroup bg = new Schema.tbl_BalanceGroup();
var qry = from g in pdc.tbl_BalanceGroups orderby g.Description select g;
this.GridView1.DataSource = qry.ToList(); this.GridView1.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}

protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlSub = (DropDownList)this.GridView1.Rows[this.GridView1.EditIndex].FindControl("ddlSubgroup");
if (ddlSub != null)
{
DropDownList ddlGroup = (DropDownList)this.GridView1.Rows[this.GridView1.EditIndex].FindControl("ddlGroup");
ddlSub.DataSource = rg.ListOfGroup(ddlGroup.SelectedValue, "");
ddlSub.DataBind();
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; LoadData();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
reportGroup rg = new reportGroup();
DropDownList ddlGroup = (DropDownList)e.Row.FindControl("ddlGroup");
ddlGroup.Items.Clear();
ddlGroup.AppendDataBoundItems = true;
ddlGroup.DataSource = rg.ListOfGroup("", "");
ddlGroup.DataBind(); ddlGroup.Items.Add("");
Label lbl = (Label)e.Row.FindControl("Label1");

if (lbl != null)
{
ddlGroup.SelectedValue = lbl.Text;
DropDownList ddlSub = (DropDownList)e.Row.FindControl("ddlSubgroup");
if (ddlSub != null)
{
ddlSub.Items.Clear();
if (lbl.Text != "")
{
ddlSub.AppendDataBoundItems = true;
ddlSub.DataSource = rg.ListOfGroup(lbl.Text, "");
ddlSub.DataBind(); ddlSub.Items.Add("");
Label lbl2 = (Label)e.Row.FindControl("Label2");
if (lbl != null)
{
ddlSub.SelectedValue = lbl2.Text;
}
}
}
}
}
else
{
Label lbl = (Label)e.Row.FindControl("Label1");
if (lbl != null)
{
lbl.Text = rg.Name(lbl.Text);
}
Label lbl1 = (Label)e.Row.FindControl("Label2");
if (lbl1 != null)
{
lbl1.Text = rg.Name(lbl1.Text);
}
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String CodeID = GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = GridView1.Rows[e.RowIndex];
Schema.DataClassesDataContext pdc = new Schema.DataClassesDataContext(cnn.getString());
Schema.tbl_BalanceGroup bg = new Schema.tbl_BalanceGroup();
var qry = from g in pdc.tbl_BalanceGroups where g.CodeAction == CodeID select g;
if (qry.Count() > 0)
{
bg = qry.First(); bg.GroupID = new Guid(((DropDownList)row.FindControl("ddlGroup")).SelectedValue.ToString());
DropDownList ddl = (DropDownList)row.FindControl("ddlSubgroup");
if (ddl != null)
{
if (ddl.SelectedValue != "")
{
bg.SubGroupID = new Guid(ddl.SelectedValue.ToString());
}
else
{
bg.SubGroupID = Guid.Empty;
}
}
pdc.SubmitChanges();
}
pdc.Connection.Close();
this.GridView1.EditIndex = -1;
LoadData();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1; LoadData();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
LoadData();
GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind();
}

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

This site uses Akismet to reduce spam. Learn how your comment data is processed.