[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Obtaining Cell value of Datagrid using Select Button on the SelectedIndexChanged Event

I have a datagrid that I want an event to fire when user hits the select button.  I have put a select button on the datagrid and then put some code in the SelectedIndexChanged event handler for that datagird.   All i am trying to do is get the keyfield value from the selected item of the datagrid.   e arg has no elements to reference cell or control values in the selected row of the data grid.   I therefore am trying the following code to try to get at the value of this keyfield.   The Keyfield is on the first cell (0).   and here is what I am to do.  I want to assign the variable "fileID" to the value of the selected row item so I can use that to pass to by SQL command.

    Private Sub DataGrid2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid2.SelectedIndexChanged
        Dim fileID As Integer
        fileID = Me.DataGrid2.SelectedItem.Cells(0).FindControl(0)????????
    End Sub

I have taken two approches.  One was to access the cell value directly.  The other was to convert that column on the datagrid to a template and put the value in a label  "lblFileID"

Since the e arg give very little to work with on this handler, what would be the best way to handle this situation.

1 Solution
Try this:
fileID = Me.DataGrid2.SelectedItem.Cells(0).Text
jholmes724Author Commented:
Thanks that work.  I forgot I had changed it to a templat column.  Once I changed it back to a normal column your code line work.  Thank you.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now