Data Going Out of Synch when using GridView inside UpdatePanel

后端 未结 1 1107
离开以前
离开以前 2021-01-22 02:12

Data in my GridView inside an UpdatePanel is going out of synch with the database and I can\'t figure out why. This has resulted in incorrect updates to the database which I hav

1条回答
  •  礼貌的吻别
    2021-01-22 02:42

    It looks like you are having some trouble with GridView and updating them. I will post a complete working example below. Start with that and gradually update that code to fit your own needs, like getting data with var query = from c in dataTable.AsEnumerable(). The important thing is to sort the data every time you (re)bind the GridView data. And I'm not sure what is happening inside resort, but you have to use dt.DefaultView.ToTable(); to save the sorting in the DataTable.

    
        
    
            
                
    
                    
                        
                            <%# Eval("ID") %>
                        
                    
    
                    
                        
                            <%# Eval("name") %>
                        
                        
                            
                        
                    
    
                
            
    
            
    
        
    
    

    Code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        //bind data in an ispostback check
        if (!IsPostBack)
        {
            DataGrid_Load();
        }
    }
    
    
    private void DataGrid_Load()
    {
        //load the datatable data
        DataTable dt = source;
    
        //check if the viewsstate existst
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            //sort the datatable before binding it to the gridview
            dt.DefaultView.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            dt.DefaultView.ToTable();
        }
    
        //bind the sorted datatable to the gridvidw
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //load the previous sorting settigns
        string sortExp = ViewState["SortExpression"] as string;
        string sortDir = ViewState["SortDirection"] as string;
    
        //reverse the direction if the column is the same as the previous sort
        if (sortDir == "asc" & sortExp == e.SortExpression.ToString())
            ViewState["SortDirection"] = "desc";
        else
            ViewState["SortDirection"] = "asc";
    
        //put the current sort column in the viewstate
        ViewState["SortExpression"] = e.SortExpression.ToString();
    
        //rebind data
        DataGrid_Load();
    }
    
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //set the edit index and rebind data
        GridView1.EditIndex = e.NewEditIndex;
        DataGrid_Load();
    }
    
    
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //reset the edit index and rebind data
        GridView1.EditIndex = -1;
        DataGrid_Load();
    }
    
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //use findcontrol to locate the textbox in the edit template
        TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
    
        //get the id of the row from the datakeys
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    
        //show result for testing
        Literal1.Text = "ID: " + id + "
    Name: " + tb.Text; //reset the edit index and rebind data GridView1_RowCancelingEdit(null, null); }

    0 讨论(0)
提交回复
热议问题