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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube