Hi,
I have a GridView that is being dynamically created based on the database.
All rows of the GridView have 2 DropDownList.
For each DropDownList the SelectedIndexChanged event is expected that update the corresponding row in the database table. After the update using the SelectedIndexChanged event of first DropDownList, in GridView RowDataBound it's set on DropDownList selected value stored in database.
When update the second DropDownList, using the SelectedIndexChanged event, are sent to the database the values of the first list DropDownList.
If I try in the GridView RowDataBound a remove in the DropDownList the value stored in the database, it works fine.
Any help would be much appreciated.
My code below
HTML
<asp:GridView ID="gvProducts"
runat="server"
DataKeyNames="ID"
EmptyDataText="No data"
EnableViewState="true"
CssClass="mGrid"
HorizontalAlign="Center"
AutoGenerateColumns="false"
Width="100%"
OnRowDataBound="gvProducts_RowDataBound">
<AlternatingRowStyle CssClass="altrows" />
<Columns>
<asp:TemplateField
HeaderText="ddl1"
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="ddl2"
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddl2" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="ddl2_SelectedIndexChanged">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string t_ddl1 = DataBinder.Eval(e.Row.DataItem, "t_ddl1").ToString();
DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddl1");
if (!String.IsNullOrEmpty(t_ddl1))
{
string sql = "SELECT t_ddl1 FROM `tb_t` t JOIN `tb_g` p " +
"ON t.CIGM = p.CIGM " +
"WHERE CIGM = ?; ";
string conString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con =
new MySqlConnection(conString))
{
using (MySqlDataAdapter sda =
new MySqlDataAdapter(sql, con))
{
sda.SelectCommand.Parameters.AddWithValue("param1", CIGM.ToString().ToUpper());
using (DataTable dt =
new DataTable())
{
sda.Fill(dt);
ddl1.DataSource = dt;
ddl1.DataTextField = "t_ddl1";
ddl1.DataValueField = "t_ddl1";
ddl1.DataBind();
ddl1.Items.FindByValue(t_ddl1).Selected = true;
ddl1.Items.Insert(0, new ListItem("[ === === === ]", ""));
ddl1.Items.Insert(1, new ListItem("OK", "OK"));
ddl1.Items.Insert(2, new ListItem("KO", "KO"));
}
}
}
}
string t_ddl2 = DataBinder.Eval(e.Row.DataItem, "t_ddl2").ToString();
DropDownList ddl2 = (DropDownList)e.Row.FindControl("ddl2");
if (!String.IsNullOrEmpty(t_ddl2))
{
string sql = "SELECT t_ddl2 FROM `tb_t` t JOIN `tb_g` p " +
"ON t.CIGM = p.CIGM " +
"WHERE CIGM = ?; ";
string conString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con =
new MySqlConnection(conString))
{
using (MySqlDataAdapter sda =
new MySqlDataAdapter(sql, con))
{
sda.SelectCommand.Parameters.AddWithValue("param1", CIGM.ToString().ToUpper());
using (DataTable dt =
new DataTable())
{
sda.Fill(dt);
ddl2.DataSource = dt;
ddl2.DataTextField = "t_ddl2";
ddl2.DataValueField = "t_ddl2";
ddl2.DataBind();
ddl2.Items.FindByValue(t_ddl2).Selected = true;
ddl2.Items.Insert(0, new ListItem("[ === === === ]", ""));
ddl2.Items.Insert(1, new ListItem("OK", "OK"));
ddl2.Items.Insert(2, new ListItem("KO", "KO"));
}
}
}
}
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
DropDownList ddl1_1 = (DropDownList)row.FindControl("ddl1");
string ddl1_2 = ddl1_1.SelectedValue;
using (MySqlConnection myConnectionString =
new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (MySqlCommand cmd =
new MySqlCommand("sp_ddl1", myConnectionString))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@sp_ddl1_2", ddl1_2.ToString());
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
}
BindGrid();
}
protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl2 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl2.NamingContainer;
DropDownList ddl2_1 = (DropDownList)row.FindControl("ddl2");
string ddl2_2 = ddl2_1.SelectedValue;
using (MySqlConnection myConnectionString =
new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (MySqlCommand cmd =
new MySqlCommand("sp_ddl2", myConnectionString))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@sp_ddl2_2", ddl2_2.ToString());
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
}
BindGrid();
}