views:

9182

answers:

2

Hello all,

I have a GridView that allows editing the values in every column, in every row, all the time. The user enters in all their changes, clicks Save once and all changes are commited.

The user must also be able to click the New button, have a new row appear in the GridView (yep, it has to show up in the actual GridView), enter whatever data they want, click Save and have all changes go to the database.

Now. Here's the user case that's throwing me: a user arrives at the page, makes several changes on several existing rows, and then needs to add a new row, enter data in the new row, click Save, and have all changes go to the database.

However, the only ways I've seen to add a new, empty row involve rebinding the GridView, which means all of their changes will be lost. This is obviously no good.

So, my question would be: what are some approaches to adding a new, empty, editable row to a GridView without having to rebind the GridView?

The only thing I can think of would be, on the New buttons' click event, suck all the data out of the GridView (including the user's potential edits), save it to ViewState (or whatever), add the new row, repopulate the grid. This, to me, seems a little hacky, but it should allow me to turn ViewState off on the GridView.

Any ideas?

+1  A: 

Just off my head I can think of two options. The first is to cache the original results that you are binding into the grid and when you need to add another row you add a datarow to the datatabale that you are binding to and then bind this to the grid. If there are changes in the grid then you need to update the datatable. Once all changes have been made and the user clicks the save button you can iterate through the table and update the DB with the data.

It might look like this

Page Loads

  • Get DB Data and put into a table
  • Bind the table to the grid
  • Store the table in a cache

When user asks for a new row

  • Get the cached data object.
  • Update any rows that have changed
  • Add an empty row Bind to the grid

When the user saves the grid

  • Get the cached object.
  • Make last set of updates
  • Loop through the row and update the DB

The other way to do this is by creating the grid dynamically but this will involve far more effort than it's worth given what you have described.

Middletone
A: 

You could dynamically add the new row via javascript, and on the save command look for newly added rows. That is fairly common.