I've done this a few times. The basic premise of my solution is that you load the data into a local collection, and store this in the ViewState of the page.
List<MyObject> lst = new List<MyObject>();
// Populate the list from the database here
// Store this list in the ViewState
ViewState["List"] = lst;
I then have a function which binds this list to the GridView, which I call in the first Page_Load, and any function which modifies this list:
function BindList() {
List<MyObject> lst = (List<MyObject>) ViewState["List"];
GridView1.DataSource = lst;
GridView1.DataBind();
}
To add a new item...
function cmdAdd_Click(object sender, EventArgs e) {
// Retrieve list from ViewState
List<MyObject> lst = (List<MyObject>) ViewState["List"];
// Add the new item
MyObject newObj = new MyObject(); // Populate this from your form
lst.Add(newObj);
// Update the list in the ViewState
ViewState["List"] = lst;
// Update the grid to show the new item
BindList();
}
When you want to persist all of the items to the database, simply retrieve the list from the ViewState.