GridView and Updating A Row Manually

G07L0095

A couple of days ago I mentioned a project that I’ve been working on that is a bit out of the ordinary as far as GridViews go.  One of the issues I’ve had is that the edit template doesn’t map to the view template very well.

OK, it doesn’t map at all.

You see, the data that gets stored back to the database during the edit could go to two of four different tables.  But the view is generated from a stored procedure that gathers the information from those tables and makes the result look like it came from one table.

So how do you have the GridView update the database under these conditions?

The secret lies in the event handlers–in this case, the RowUpdating event handler.

Using the RowUpdating handler, we can do whatever we want.  The first line of code you need to make sure you include is

e.Cancel = true;

“e” represents the System.Web.UI.WebControls.GridViewUpdateEventArgs object that is passed in.  What the line of code says is “cancel any other update processing that may have happened after this event gets called.”

From here on out, you’ll need to retrieve the values from your edit controls manually  using the FindControByID() method I demonstrated on Monday and updating the database using code.

 

Other places talking about the GridView and Updating a Row

Programming Shorts : In GridView RowUpdating Event Handler, e … – I use custom data objects to populate data controls. Typically, I’ll create a factory object and factory method that returns a List collection of "ConcreteProducts". For example, RegistrationRuleFactory f = new …

sitecore with gridview – rowupdating event doesn’t fire – gridview to in web.config resolves this issue. your web.config would then look like : system.web.ui.webcontrols.repeater system.web.ui.webcontrols. …

in gridview rowupdating event handler, e.newvalues and e.oldvalues … – in the code behind, handle the gridview’s rowupdating event and access the form data as such: /// /// handle the row update event /// void rulesgridview_rowupdating(object sender, gridviewupdateeventargs e) …

DataBinding in ASP.NET 2.0 and the RowUpdating event – Dennis van … – For a while now I’m trying to figure out why my method, triggered by the GridView.RowUpdating event, doesn’t work as all samples say it should do. All samples of course assume you’re doing everything in your .aspx page, but I have to do …

GridView & RowUpdating — Or Dude, where are my new values? – Part of the consequence of that is that the GridView loses part of its functionality, like the RowUpdating event doesn’t provide OldValues and NewValues, instead they are blank. So here’s a scenario. You have a list, like the following …

Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView … – The above block of codes in RowUpdating event, finds the control in the GridView, takes those values in pass it to the CustomersCls class Update method. The first parameter GridView1.DataKeys[e.RowIndex].Values[0]. …

Related Post

  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it wrong, which...
  • Editing in a GridView without switching to Edit mode.Editing in a GridView without switching to Edit mode. I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go... Here's the problem.  You want to be able to edit all of the rows i...
  • Sharing ObjectDataSource From CodebehindSharing ObjectDataSource From Codebehind I ran into an interesting “problem” last week that wasn’t all that hard to figure out once I broke out the debugger.  But I thought it would be useful to share the results to save others th...
  • Conditional Footnotes On GridViewConditional Footnotes On GridView A couple of days ago I ran into a requirement to conditionally display footnote information at the bottom of a GridView display.  We were already conditionally displaying icons in the row.  What w...
  • GridViews – Multiple Rows Per RecordGridViews – Multiple Rows Per Record I don't know about you, but I've had several occasions where I've needed to use the simplicity of the GridView control in ASP.NET.  DataBinding, Paging, Sorting, etc.  But I've also wanted to stac...