GridView动态添加一行 点击添加按钮之后 DropDownList控件选择的值消失,怎麼办?
展开全部
在行添加结束事件里,对下拉框数据源绑定
追问
有没有代码可以参考一下。
追答
privatevoid LoadGrid()
{
try
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Product Name");
DataColumn dc2 = new DataColumn("Price");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception)
{
throw;
}
}
protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable products = (DataTable)GetProducts();//GetProducts() is the Method to Get Products list
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
ddl.DataSource = products;
ddl.DataBind();
e.Row.Cells[1].Text = "";
}
}
protectedvoid DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)GridView1.Rows[0].FindControl("DropDownList1");
GridView1.Rows[0].Cells[1].Text = ddl.SelectedValue;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询