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 = _

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 = _
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 = _
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

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

comments powered by Disqus


  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube