dropdown in gridview dependent on another dropdown.
here is what i tried
aspx
<asp:GridView ID="gvProgram" runat="server" DataKeyNames="ChoiceName" AutoGenerateColumns="False" OnRowDataBound="gvProgram_RowDataBound" Width="100%" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:BoundField DataField="ChoiceName" HeaderText="Choice" ItemStyle-Width="20%" />
<asp:TemplateField HeaderText="Program">
<ItemTemplate>
<asp:DropDownList ID="ddlProgram" runat="server" class="form-control form-control-sm mb-9" OnSelectedIndexChanged="ddlProgram_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="0">Select Program</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvddlProgram" runat="server" ErrorMessage="*" ControlToValidate="ddlProgram" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" InitialValue="0"></asp:RequiredFieldValidator>
<%-- <a href="#" target="_blank">BA Economics</a>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:DropDownList ID="ddl_Location" runat="server" class="form-control form-control-sm mb-9">
<asp:ListItem Value="0">Select Location</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvddlLocation" runat="server" ErrorMessage="*" ControlToValidate="ddl_Location" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" InitialValue="0"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#6777ef" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
code behind file
protected void gvProgram_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataSet ds = new DataSet();
Btech.Mode = "BindData";
ds = objDal.Adm(Btech);
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (ds.Tables[0].Rows.Count > 0 && ds.Tables[1].Rows.Count > 0)
{
DropDownList ddlProgram = (DropDownList)e.Row.FindControl("ddlProgram");
ddlProgram.Items.Clear();
ddlProgram.DataSource = ds.Tables[13];
ddlProgram.DataTextField = "Description";
ddlProgram.DataValueField = "CourseID";
ddlProgram.DataBind();
ddlProgram.Items.Insert(0, new ListItem("Select course", "0"));
DropDownList ddl_Location = (DropDownList)e.Row.FindControl("ddl_Location");
//ddlTeacherNames.Items.Clear();
ddl_Location.DataSource = ds.Tables[14];
ddl_Location.DataTextField = "CenterName";
ddl_Location.DataValueField = "CentreCode";
ddl_Location.DataBind();
ddl_Location.Items.Insert(0, new ListItem("Select Location", "0"));
}
}
}
```
protected void ddlProgram_SelectedIndexChanged(object sender, EventArgs e)
{
}
Ok, there are several things we have to deal with.
First, the GridView is loaded.
Then we have to load the 2 combo boxes, but ALSO setup the correct cascade values for the 2 combo boxes. And THEN we have to set the correct value of the combo box based on the row data source. So, in theory there are 3 steps here. And this step is somewhat backwards, since that 2nd combo box selection and value has to setup the correct values in the first combo box.
Then, after all above is done, then we need to setup the combo boxes to cascade correctly WHEN the user selects the first combo box, and cascade to the 2nd in that one GridRow.
Often, this will suggest that the 1st combo box is NOT bound to any data in the given row, but ONLY the 2nd combo box is to be bound to the given row data.
I don't have your sample data, but let's assume some people, and we select the city, and then the 2nd cascaded combo box is the hotel they selected.
So, first our GridView markup:
I will also point out that I don't bother with the GridView events, and I suggest you don't bother either. So, first the code to load the combo boxes.
Ok, so above load the GridView. However, as noted, while we have 2 combo boxes (select city, then select a hotel in that given city list), ONLY the 2nd combo box selection need be saved.
So, in Row data bound, we have to not only load the 2 combo boxes, but correctly set the city choice, and then load hotels based on that hotel, and then select the Hotel.
So, above sets up everything. Next up is the cascade of the city selection. So, note in above, the markup for the combo box is this:
Note how the auto post back = true. And note the event setting for the combo box.
So, the city combo box changed event is this:
And of course, after the user makes changes, then we need to save the changes. So, our save button is this:
And the result is this: