Practical ASP.NET

Getting Data Out of the GridView (and the Other Views, Too)

Peter answers a reader's question by showing how to access data inside the individual controls of the various DataViews.

Recently, a reader asked me how to get at the controls inside a GridView during the RowDataBound event. The answer is worth enough of an explanation to hang a column on. I'll start with the general case and finish by actually answering the question (and by the way, if you have a question, don't hesitate to post a comment or e-mail me).

Retrieving Display Data
The answer depends on two things: the state of GridView and whether the column you want is templated. For instance, to retrieve the value of a cell that isn't in edit mode and is in a column that isn't templated, you can use the view's Rows and Cells collection to access any Cell. Once you've got the Cell you want, the Cell's Text property gives you the value:

Dim Region As String = _
   Me.GridView1.Rows(1).Cells(1).Text

This code also works for the DetailsView.

Of course, in a GridView you probably don't want just any row -- you want the row that contains the data the user wants to process. In the DetailsView, this isn't a problem because only one record is displayed at a time. In a GridView, you need to give the user the ability to "mark" a row.

The simplest solution is to check off Enable Selection on the GridView's SmartTag, which adds a Select button to every row. When the user clicks on the Select button for a row, you can access that row through the GridView's SelectedRow property. With a row selected, the code to pull out a particular Cell's value you just specify the Cell:

If Me.GridView1.SelectedRow IsNot Nothing Then
   Region As String = _
       Me.GridView1.SelectedRow.Cells(1).Text
End If

For the DetailsView (or the GridView, if you've enabled Selection), you can bypass using the Cells collection but only for one column. At design time, set the View's DataKeyNames property to the name of the field you want. Then, at run time you can retrieve the value of the field for the selected row through the view's SelectedValue property (in a GridView the user must select a row first):

If Me.GridView1.SelectedRow IsNot Nothing Then
   Region As String = _
       Me.GridView1.SelectedValue
End If

Edit Mode and Templated Columns
But what if the GridView row is in edit mode? When a row is in Edit mode, the Cells become populated with some control (usually a text box). For this scenario, you need to go one step further and retrieve the control inside the Cell and cast it to the appropriate type. For a GridView, the EditIndex property will tell you which row is currently being edited. Fortunately, there's only one control in a cell, so it's always Controls(0) that you want, as seen here.

But what if you've turned the column into a template column and replaced the control in the cell? In that case, since you know the name of the control, I'd use the FindControl method. For a TextBox named NameTextBox in a templated column, this code will work.

Since the FormView is, essentially, nothing but templates, you should always use FindControl.

In Events
That leaves me back at the original question: How to access a row in the RowDataBound event of a GridView? Many of the GridView events pass you the selected row as one of the properties on the e parameter. You can use this property as you would the SelectedRow:

FirstName = e.Row.Cells(1).Text

Other events won't pass you the selected Row but will pass you the index of the affected row. For instance, the GridViewRowEditing event's e parameter passes the NewEditIndex property (though, it appears to be off by 1 so you need to increment it before using). You can use these indexes with the Rows property:

FirstName = Me.GridView1.Rows
   (e.NewEditIndex+1).Cells(3).Text

Other events are even more obliging: The RowUpdating and RowUpdated events, for instance, pass the values of the cells in a collection called NewValues.

As I said at the start, the correct answer is, "It depends."

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