Hi.
The problem is that
gv_pm_DataBound
event is fired each time when you refresh data in Grid. When you call:
gv_pm.DataSource = rslt.ToList();
gv_pm.DataBind();
it refreshes everything, including footer DropDownList
drp_pmm
and sets SelectedIndex to -1.
So after selecting new value in the
drp_state
you refresh the footer DropDownList as well (
gv_pm_DataBound
is called after selecting something in the
drp_state
dropdownlist). To avoid this you have to save the selected value from
drp_pmm
before refreshing data, and then set it after, check the code below, I used local variable, called
_selected_drp_pmm
for this purpose.
To resolve the issue you can try to save the selected value from the
drp_pmm
and then set it after binding the data. Please, pay attention to the code highlighted in bold and
drp_state_SelectedIndexChanged
event handler, which was also changed.
protected void drp_state_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
private int _selected_drp_pmm = -1;
void BindGrid()
{
if (gvItemsTest.FooterRow != null)
{
DropDownList ddlTestPostback = gvItemsTest.FooterRow.FindControl("ddlTestPostback") as DropDownList;
_selected_drp_pmm = ddlTestPostback.SelectedIndex;
}
using (QTEL_Entities qtl = new QTEL_Entities())
{
var rslt = from tab in qtl.STATION_PM
where tab.ID_STATION == drp_state.SelectedValue
select tab;
gv_pm.DataSource = rslt.ToList();
gv_pm.DataBind();
}
}
protected void gv_pm_DataBound(object sender, EventArgs e)
{
DropDownList drp_pmm = gv_pm.FooterRow.FindControl("drp_pmm") as DropDownList;
using (QTEL_Entities qtl = new QTEL_Entities())
{
var pm = from p in qtl.PARAMETRE_MESURE
orderby p.ID_PM
select new
{
id = p.ID_PM,
name = p.LIBELE_PM
};
drp_pmm.DataSource = pm.ToList();
drp_pmm.DataTextField = "name";
drp_pmm.DataValueField = "id";
drp_pmm.DataBind();
drp_pmm.Items.Insert(0, new ListItem("Parametre de mesure", ""));
ddlTestPostback.SelectedIndex = _selected_drp_pmm;
}
}
Suggestion, if you need to resolve some very difficult issue, try to create simple case and check out if it works. Divide your issue into parts and investigate these parts one by one. Try to create simple project with grid and dropdownlist and see if it works.
Always try to divide into parts and simplify the difficult problem as much as possible. This way you would be able to find a solution.
One question, if the
drp_pmm
is not dependent on data, why you put it in the footer? You can locate it outside of the Grid
gv_pm
.