编辑下拉列表中的C#GridView显示当前设置的项目

时间:2018-06-01 15:19:26

标签: c# dropdown

我的网站解决方案中有一个gridview,它有一个模板字段,当你点击编辑时,下拉列表出现在一列中,其中包含一组来自数据库的值。这会在更新按钮单击时设置值。但是,当我返回更新到另一个值时 - 下拉重置因此不会显示当前设置的值。

ASPX:

                <asp:TemplateField HeaderText="Exception">
                 <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ExceptionDesc") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                   <asp:DropDownList ID="ddlException" runat="server" OnSelectedIndexChanged="gvCaseList_SelectedIndexChanged"></asp:DropDownList>
                </EditItemTemplate>

            </asp:TemplateField>

编辑活动:

protected void gvCaseList_edit(object sender, GridViewEditEventArgs e)
{

    gvCaseList.EditIndex = e.NewEditIndex;
    GetgvCaseListData();


}

RowdataBound代码:

    protected void gvCaseList_RowDataBound(object sender, GridViewRowEventArgs e)
{

    MeasurementID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MeasurementID"));
    string connectionstring = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    SqlConnection cn = new SqlConnection(connectionstring);
    if (e.Row.Cells[10].Text == "OVERDUE")
    {
        e.Row.Cells[10].ForeColor = System.Drawing.Color.Red;
        e.Row.Cells[10].Font.Bold = true;
    }
    if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
    {  
        cn.Open();  
        DropDownList DropDownList1 = (e.Row.FindControl("ddlException") as DropDownList);
        SqlCommand cmd = new SqlCommand("[dbo].measurement", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@SubTeamNo", SubTeam);
        cmd.Parameters.AddWithValue("@MeasurementID", MeasurementID);

        DataTable dt = new DataTable();
        using (SqlDataAdapter a = new SqlDataAdapter(cmd))
        {
            a.Fill(dt);
        }

        cn.Close();    
        DropDownList1.DataSource = dt;          
        DropDownList1.DataTextField = "ExceptionDesc";
        DropDownList1.DataValueField = "ExceptionID";
        DropDownList1.DataBind();  
        DropDownList1.Items.Insert(0, new ListItem("--Select Exception--", "0"));


    }

}

Before edit button clicked, value has been set

After edit button clicked, drop down resets and doesnt display current item as default

0 个答案:

没有答案