Asp.net 将dropdownlist绑定到编辑时gridview上ItemTemplate中已显示的值
我必须将dropdownlist值绑定到编辑模板。下面是我的dropdownlist的代码Asp.net 将dropdownlist绑定到编辑时gridview上ItemTemplate中已显示的值,asp.net,c#-4.0,Asp.net,C# 4.0,我必须将dropdownlist值绑定到编辑模板。下面是我的dropdownlist的代码 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { //Check if this is our Blank Row being databound, if so make the row invisible if (e.Row.RowType == DataControlRowTy
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Check if this is our Blank Row being databound, if so make the row invisible
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((DataRowView)e.Row.DataItem)["CertificateNo"].ToString() == String.Empty) e.Row.Visible = false;
//Check if is in edit mode
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DropDownListStatus = (DropDownList)e.Row.FindControl("DropDownListStatus");
//Bind status data to dropdownlist
DropDownListStatus.DataTextField = "Status";
DropDownListStatus.DataValueField = "Status";
DropDownListStatus.DataSource = RetrieveStatus();
DropDownListStatus.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
DropDownListStatus.SelectedValue = dr["Status"].ToString();
}
}
}
private DataTable RetrieveStatus()
{
//fetch the connection string from web.config
string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//SQL statement to fetch entries from products
string sql = @"Select distinct Status from cpd_certificates";
DataTable dtStatus = new DataTable();
//Open SQL Connection
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
//Initialize command object
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//Fill the result set
adapter.Fill(dtStatus);
}
}
return dtStatus;
}
上面的代码填充我的dropdownlist。但是,当我单击“编辑”时,不会填充我的下拉列表。
这是我的下拉列表代码:
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<ItemTemplate><%# Eval("Status")%></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStatus" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownListStatus" runat="server" >
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
快速浏览一下您的代码,就会发现在将值分配给dropdownlist的顺序中有错误。首先将数据源分配给dropdownlist,然后在分配数据源后分配datavalue-字段和datatext字段属性
DropDownListStatus.DataSource = RetrieveStatus();
DropDownListStatus.DataTextField = "Status";
DropDownListStatus.DataValueField = "Status";
谢谢你的快速回复。订单没有问题。我只是觉得我的编辑链接错了。单击“编辑”时,要编辑的行是上一页的行。