首页 > 解决方案 > Gridview 保存数据会失去旧值

问题描述

我从数据库中获取数据。到下拉列表,如果某些值为空,那么我将其他数据绑定到绑定DropDownList的行数据中。在按钮上保存GridView数据时,单击新的选定值仅更新。丢失旧数据值。

一切正常,除了丢失旧数据值

protected override void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       PopulateLeaveAssignList();
    }
}

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView dr = e.Row.DataItem as DataRowView;
        shiftMgr = new ShiftManagerBL(BasePage.Provider, BasePage.ConnectionString);
        DataTable table = shiftMgr.GetAllShifts(userId);

        DropDownList DropDownList51 = (DropDownList)e.Row.FindControl("DropDownList51");
        DropDownList DropDownList52 = (DropDownList)e.Row.FindControl("DropDownList52");

        if (!string.IsNullOrEmpty(dr["S1"].ToString()))
        {
            DropDownList51.DataSource = table;
            DropDownList51.DataBind();
            string a = dr["S1"].ToString(); 
            DropDownList51.SelectedItem.Text = dr["S1"].ToString();
        }
        else
        {
            DropDownList51.DataSource = table;
            DropDownList51.DataTextField = "shift_title";
            DropDownList51.DataValueField = "shift_id";
            DropDownList51.DataBind();
        }

        if (!string.IsNullOrEmpty(dr["S2"].ToString()))
        {
            DropDownList52.DataSource = table;
            DropDownList52.DataBind();
            DropDownList52.SelectedItem.Text = dr["S2"].ToString();
        }
        else
        {
            DropDownList52.DataSource = table;
            DropDownList52.DataTextField = "shift_title";
            DropDownList52.DataValueField = "shift_id";
            DropDownList52.DataBind();
        }
    }
}

protected void UpdateAllCustomer(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        Rs.S1 = ((DropDownList)row.FindControl("DropDownList51") as DropDownList).SelectedItem.Value;
        Rs.S2 = ((DropDownList)row.FindControl("DropDownList52") as DropDownList).SelectedItem.Value;
    }
}

我需要保存旧DropDownList值以及新更改的值

标签: c#asp.net

解决方案


推荐阅读