简体   繁体   中英

Can't access Cell data in DataGridView VB.Net

I've got a project in Visual Studio 2017, using VB.NET that populates a DataGridView using a DataTable. The data appears fine in the grid, but when I try to access the cell value in the SelectionChanged event, I always get Nothing. Here's the event:

    Private Sub grdBooks_SelectionChanged(sender As Object, e As EventArgs) Handles grdBooks.SelectionChanged
    If Not grdBooks.CurrentRow Is Nothing Then
        If grdBooks.CurrentRow.Index > -1 Then
            Dim X As String = grdBooks.Rows(0).Cells(0).Value
            Dim Y  = grdBooks(0, 0).Value
            UpdateEditPanel(False)
        End If
    End If
End Sub

So both X and Y should give me the same value from the grid at position 0,0 but they don't. I've used this before in other projects. Does anybody have any ideas?

Thanks, Colin

I am a bit confused by this post aswell but I hope this helps you resolve the problem. Possible solution #1

Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString 

or

Dim X As String = DataGridView1.SelectedRows(0).Cells(0).Value.ToString() 

If you are able to provide more code, I'm sure we can resolve this Issue. Cheers.

If you just want to click the cell from the Grid, try this:

Private Sub grdBooks_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdBooks.CellContentClick

    Dim selectedRow As DataGridViewRow

    selectedRow = grdBooks.Rows(e.RowIndex)

    Dim x as string = selectedRow.Cells(0).Value.ToString()


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM