John Croson's Blog Home: Find GridView Cell Values on RowCommand with Paging Enabled

Friday, April 05, 2013

Find GridView Cell Values on RowCommand with Paging Enabled

This was confounding me this morning until I found this post on the ASP.NET forums.

I was setting up a GridView and needed to check the values of a couple of cells before I let users click a button in an ItemTemplate.

I initially used the Container.DateItemIndex method to determine which row they clicked but quickly found out that this only works without Paging Enabled as noted on the MSDN.


All this does is tell you the index of the data, but since the GridView index needs to be reset on each page (0-your page size) you'll never find your values.

Instead set is thusly:


You'll be able to easily access your cells then:

                    Dim index As Integer = Int32.Parse(e.CommandArgument.ToString())
                    Dim row As GridViewRow = GV_OrderHeaders.Rows(index)
                    Dim ifs As String = DirectCast(row.FindControl("lblIfsId"), Label).Text
                    Dim oid As String = DirectCast(row.FindControl("lblOrderId"), Label).Text
                    Dim qid As String = GV_OrderHeaders.DataKeys(index).Value

No comments: