Editable GridView that saves changes to viewstate before saving

I know how to have an editable GridView together with SqlDataSource, in which every edit (update / insert / delete) is immediately saved in the database (using SqlDataSource UpdateCommand, Insertcommand, etc.).

Now I need to have an editable GridView that saves all changes to the viewstate until the user clicks the Save button in another place on the form.

In other words:

  • At first boot, enter GridView data from the database
  • The user makes various changes for data that is not yet stored in the database, but which survive through any number of postbacks.
  • The user clicks Save, and all changes are saved in DB

I assume that I will need to write my own code to save the data in step 3, but is there a simple, ready-made approach to step 2?

+5
source share
2 answers

You want to use a DataSet or DataTable and use the following:

myDataSet.AcceptChanges();

This captures the changes when this method is called in a DataSet, DataTable or DataRow. Think of it almost like SqlTransaction, where you need to commit or rollback. Hope this helps!

see link: Accept changes

+2
source

I can suggest doing the following:
1) create a custom list object in which your data is stored. Load DB data into this object and save it in session state.

An object:

  public class InvestorClaim  
  {  
    public InvestorClaim()  
    {  
    }

    private int? _record_id;
    private int? _ic_record_id;
    private Int64? _lh_record_id;

    public int? record_id
    {
      get { return _record_id; }
      set { _record_id = value; }
    }

    public int? ic_record_id
    {
      get { return _ic_record_id; }
      set { _ic_record_id = value; }
    }

    public Int64? lh_record_id
    {
      get { return _lh_record_id; }
      set { _lh_record_id = value; }
    }  
} 

:

List<InvestorClaim> inv_claim = new List<InvestorClaim>();  

inv_clai= dataFromDB

:

 HttpContext.Current.Session[ "InvestorClaimsObject" ] = inv_claim;

2) gridview .

  protected void yourGridView_Bind()  
  {   
    inv_claim = HttpContext.Current.Session[ "InvestorClaimsObject" ] as List<InvestorClaim>;  
    yourGridView.DataSource = inv_claim;  
    BindData();   
  }  

  protected void yourGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  {      
    inv_claim = HttpContext.Current.Session[ "InvestorClaimsObject" ] as List<InvestorClaim>;  

    //Update the values.
    GridViewRow row = yourGridView.Rows[e.RowIndex];
    inv_claim[row.DataItemIndex].lh_record_id = ((TextBox)(row.Cells[1].Controls[0])).Text;
    inv_claim[row.DataItemIndex].ic_record_id = ((TextBox)(row.Cells[2].Controls[0])).Text;

    //Reset the edit index.
    yourGridView.EditIndex = -1;

    //Bind data to the GridView control.
    yourGridView.DataSource = inv_claim;
    BindData();  
}

3) .

+1

All Articles