Herb Caudill
Herb Caudill

Reputation: 49972

Editable GridView that stores changes in viewstate until save

I know how to have an editable GridView along with a SqlDataSource in which each edit (update/insert/delete) is immediately persisted to the database (using the SqlDataSource's UpdateCommand, Insertcommand, etc).

What I need now is to have an editable GridView that maintains all edits in viewstate until the user presses a "Save" button elsewhere on the form.

In other words:

  1. On first load, populate the GridView from DB data
  2. User makes various edits to the data, which are not persisted to the DB yet, but which survive through any number of postbacks.
  3. User presses Save, and all the changes are persisted to the DB

I assume I'll need to write custom code to persist the data in step 3, but is there a straightforward, out-of-the-box approach to step 2?

Upvotes: 5

Views: 5279

Answers (2)

R_F
R_F

Reputation: 31

I can suggest to do the following:
1) create a custom list object that stores your data. Upload DB data to that object and save it to session state.

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; }
    }  
} 

Upload data to the list :

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

inv_clai= dataFromDB

Save to session :

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

2) bind gridview to the object and manipulate data as you need.

  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) save data from session list object to DB when ready.

Upvotes: 1

mirezus
mirezus

Reputation: 14246

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

myDataSet.AcceptChanges();

This commits the changes when you call that method on the DataSet, DataTable, or DataRow. Think of this almost like a SqlTransaction where you need to commit or rollback. Hope this helps!

see link: Accept Changes

Upvotes: 2

Related Questions