Hi all,
I want to display "Area" value from database in gridview header named "Area" and then filter the record based on selected dropdown value in header class and section.
I'm trying to customize this example
Bind(Populate) DropDownList in GridView HeaderTemplate in ASP.Net using C# and VB.Net
The gridview populates with the value selected from the dropdownlist, but this part doesn't work at all because the value chosen from the dropdownlist doesn't remain selected
private void BindCities(DropDownList myDDL)
{
myDDL.DataSource = GetDataTable(" SELECT DISTINCT ....; ");
myDDL.DataTextField = "myArea";
myDDL.DataValueField = "myArea";
myDDL.DataBind();
ViewState["SelectedCity"] = string.Empty;
ViewState["SelectedCity"] = null;
ViewState["SelectedCity"] = myDDL.SelectedValue;
if (ViewState["SelectedCity"].ToString() != "SELECT")
{
myDDL.Items.FindByText(ViewState["SelectedCity"].ToString()).Selected = true;
}
}
My complete code below, thanks.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ViewState["SelectedCity"] = "SELECT";
BindData3();
}
private void BindData3()
{
GridView3.DataSource = RetrieveProducts3();
ViewState["SelectedCity"] = "SELECT";
GridView3.DataBind();
}
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
DropDownList myDDL = (DropDownList)e.Row.FindControl("myDDL");
BindCities(myDDL);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
private void BindCities(DropDownList myDDL)
{
myDDL.DataSource = GetDataTable(" SELECT DISTINCT ....; ");
myDDL.DataTextField = "myArea";
myDDL.DataValueField = "myArea";
myDDL.DataBind();
ViewState["SelectedCity"] = string.Empty;
ViewState["SelectedCity"] = null;
ViewState["SelectedCity"] = myDDL.SelectedValue;
if (ViewState["SelectedCity"].ToString() != "SELECT")
{
myDDL.Items.FindByText(ViewState["SelectedCity"].ToString()).Selected = true;
}
}
protected void myDDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList myDDL = (DropDownList)sender;
ViewState["SelectedCity"] = myDDL.SelectedValue;
BindData3();
}
<asp:TemplateField>
<HeaderTemplate>
<asp:DropDownList ID="myDDL" runat="server"
AutoPostBack="true"
AppendDataBoundItems="true"
OnSelectedIndexChanged="myDDL_SelectedIndexChanged">
<asp:ListItem Text="[ === === ]" Value="SELECT"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("myArea") %>
</ItemTemplate>
</asp:TemplateField>