Practical ASP.NET

Databinding without Tables: Updates and Deletes

Peter adds the code to support doing updates and deletes with an unbound GridView.

In last week's column (Databinding the GridView Without Using a Table), I looked at working with the GridView in an "unbound" mode. I was doing this because I need to display multiple items of data that I was retrieving from a string property on a middle-tier business object (i.e. data available only in memory at runtime). By the end of last week's column, I was extracting some XML data from the middle-tier object, loading that XML into a typed DataSet (which I called Parameters) and displaying the data in a GridView.

This week, I'll look at the code required to support changing and deleting records. It's here that I'll reap the benefits of converting my XML data into a DataTable in a DataSet.

Handling Update
To support updating, I needed to add code to the events that are fired by the GridView's Edit, Update, and Cancel buttons.

The Edit button fires the RowEditing event. In that event, I needed to set the GridView's EditIndex property to have the right row display using its EditItemTemplate (which I had the GridView generate in last week's column). Fortunately, the e parameter passed to this event reports the row the user is editing through its NewEditIndex property. After setting the EditIndex property, I have to reset the DataSource using the copy of my DataSet retrieved from my LoadData method (discussed last week), and rebind the GridView:

With Me.GridView1
.EditIndex = e.NewEditIndex
.DataSource = LoadData()
.DataBind()
End With

After this code executes (and the page redisplays) the user will have a row of textboxes where he or she can enter data and two buttons: Update and Cancel.

To support the Cancel button, I must add code to the GridView's RowCancelingEdit event. The only difference between this code and the code in the RowEditing event is that I have to set the EditIndex property to -1, so that no rows will display using their EditItemTemplate:

With Me.GridView1
.EditIndex = -1
.DataSource = LoadData()
.DataBind()
End With

The Update button fires the RowUpdating event. Handling data changes in this event isn't very complicated, because I'm using a DataSet to hold my data. In the event, I first need to recreate my DataSet and set the Name and Values fields in the Parameter table using values from the corresponding cells in the GridView. The RowIndex property on the event's e parameter tells me which row to update:

Dim ds As Parameters = LoadData()
With ds
.Parameter(e.RowIndex).Name =
CType(Me.GridView1.Rows(e.RowIndex).Cells(2).Controls(1),
TextBox).Text
.Parameter(e.RowIndex).Value =
CType(Me.GridView1.Rows(e.RowIndex).Cells(3).Controls(1),
TextBox).Text

Having updated the DataSet, I need to save it somewhere. I do that with a method called SaveData (discussed later in this article). Once the DataSet is saved, I set the GridView to have no rows in edit mode and rebind it to the DataSet:

SaveData(ds, True)
With Me.GridView1
.EditIndex = -1
.DataSource = ds
.DataBind()
End With

Dealing with Deletes
Clicking the Delete button fires the RowDeleting event. In this event, I again retrieve my Parameter DataSet. Once I have the DataSet, I delete the row indicated by the RowIndex property of the event's e parameter and call the DataSet's AcceptChanges method to flush the deleted row. Here again, I use my SaveData routine to save the updated DataSet before rebinding it to the GridView:

Dim ds As Parameters = LoadData()
ds.Parameter(e.RowIndex).Delete()
ds.AcceptChanges()
SaveData(ds, True)
With Me.GridView1
.DataSource = ds
.DataBind()
End With

The SaveData method is simpler than the LoadData method I discussed in last week's column. In the SaveData method, I extract the XML from the DataSet passed to the method and stuff it into the Page's ViewState to be used later. If the second parameter passed to the method is True, I also update the object that holds the data:

Sub SaveData(ByVal ds As Parameters, ByVal Update As Boolean)
Me.ViewState("XML") = ds.GetXml
If Update Then
Dim cmd As New MyCommandObject(cmdId)
cmd.Parameters = ds.GetXml
End If
End Sub

I'll discuss the reason for controlling updates to the business level object in next week's article when I cover doing inserts with the GridView.

All that's left now is to handle adding new records, which is next week's topic, along with returning to the sorting problem discussed in my earlier column, titled Sorting in the ObjectDataSource.

Finally, a tip of the hat to Robin Hood, for doing much of the work that I'm taking credit for here.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Mastering AI Development and Building AI Apps with GitHub Copilot

    Two Microsoft experts explain how GitHub Copilot is evolving from a coding assistant into a broader platform for building, customizing and testing AI-powered developer workflows.

  • VS Code 1.123 Adds Agent Session Sync, 1M Context Windows

    Microsoft released Visual Studio Code 1.123 on June 3, adding agent-focused features, larger model context support, integrated browser updates and a new delay for some automatic extension updates.

  • Copilot Billing Shock Hits Developers

    Developer complaints about GitHub Copilot's new usage-based billing model have centered on unexpectedly rapid AI credit consumption, and neither GitHub nor Microsoft has responded directly to the backlash, though they have previously published guidance to lessen model usage costs.

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

Subscribe on YouTube