error in get data from access database to datagridview Vb.Net

后端 未结 1 1869
醉酒成梦
醉酒成梦 2021-01-27 11:32

i have a problem when i click to item in datagridview to get more information ! ok ? my code :

Try
        If (DataGridView1.Rows.Count = 0) Then Return
                 


        
相关标签:
1条回答
  • 2021-01-27 12:26

    You are trying to access the DataGridView like an array, when you need to specify that you're looking in the rows and columns. Instead of

    DataGridView1(2, DataGridView1.SelectedRows(0).Index).Value
    

    you can write

    DataGridView1.Columns(2).Cells(DataGridView1.SelectedRows(0).Index).Value
    

    However, relying on SelectedRows is not the best way to do it. What if the user accidentally selected a few rows and clicked the bottom one?

    Assuming your code is in a CellClick handler with

    (sender As Object, e As DataGridViewCellEventArgs)
    

    you should use the DataGridViewCellEventArgs to tell what row you're on instead of relying on SelectedRows:

    Dim id As String = DataGridView1(e.RowIndex).Cells(2).Value 
    
    0 讨论(0)
提交回复
热议问题