我有一个带有员工ID的gridview和旁边的编辑按钮。单击编辑按钮时,我需要显示一个下拉列表,该列表将填充db。
中的数据<asp:GridView ID="grvEmp" runat="server" Width="100%" GridLines="Both"
HeaderStyle-BackColor="#999999" AutoGenerateColumns="false"
OnRowEditing="grvEmp_RowUpdating" OnRowUpdating="grvEmp_RowUpdating">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Employee Name
</HeaderTemplate>
<ItemTemplate>
<%# Eval("EmployeeID")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnLoad="DropDownList1_onload"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server" CommandName="edit" /><br />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#999999" />
</asp:GridView>
//代码隐藏
protected void grvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source=CHANGEME1;Initial Catalog=Reflection;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("select salary from employee",cn);
DataSet ds = new DataSet();
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); //null-unable to capture the control
da.Fill(ds);
cn.Close();
ddl.DataSource = ds;
ddl.DataTextField = "salary";
ddl.DataValueField = "salary";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
但是当点击编辑按钮时,不会填充下拉列表。 任何人都可以提供这方面的见解吗?
答案 0 :(得分:2)
您需要检查该行是否为datarow且处于编辑模式。您的RowDataBound方法应如下所示:
protected void grvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source=CHANGEME1;Initial Catalog=Reflection;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("select salary from employee", cn);
DataSet ds = new DataSet();
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); //null-unable to capture the control
da.Fill(ds);
cn.Close();
ddl.DataSource = ds;
ddl.DataTextField = "salary";
ddl.DataValueField = "salary";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
编辑:
你应该在RowEditing中有这样的东西:
protected void grvEmp_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
grvEmp.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
编辑2:您应始终按照John Saunders在评论中的建议将Using
中的连接和数据适配器包装起来,这将自动管理连接和数据适配器的生命周期,并在何时关闭/处置它超出了范围。
using (SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source=CHANGEME1;Initial Catalog=Reflection;Integrated Security=True"))
{
cn.Open();
using (SqlDataAdapter da = new SqlDataAdapter("select salary from employee", cn))
{
DataTable dt = new DataTable();
da.Fill(dt);
ddl.DataSource =dt;
ddl.DataTextField = "salary";
ddl.DataValueField = "salary";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}