2
votes

The first time I populate my datagridview it highlights the first row and I have a button that, when clicked, shows the value of the index that was clicked so I know which row was clicked. If the user does another search, I clear out the contents of the datagridview and then repopulate it with new data. Again, the first row (index(0)) is highlighted, but this time, when the user clicks the button, even though the first row of the grid is highlighted, I get an error showing that data_grid.rows is nothing. I've tried setting the first row to .selected = true, but this doesn't change anything. It only works the first time, then it never works again. Here is my code:

Public sub show_data()

 ' Clear everything out of the data grid
 ' -------------------------------------
   With data_grid
     While data_grid.RowCount > 0
       data_grid.Rows.Clear()
     End While
     While data_grid.ColumnCount > 0
       data_grid.Columns.Clear()
     End While
   End With

 ' add the empty rows to the data grid
 ' -----------------------------------
  Dim rows As Integer = assessment_array.Count()
  data_grid.Rows.Add(rows)

' Add the data to the columns
' ---------------------------
  For r As Integer = 0 To rows - 1
   data_grid.Item(0, r).Value = assessment_array(r)(0) 
   data_grid.Item(1, r).Value = assessment_array(r)(1) 
   data_grid.Item(2, r).Value = assessment_array(r)(2)
  Next

 With data_grid
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    .RowHeadersVisible = False 
    .AllowUserToAddRows = False 
    .AllowUserToDeleteRows = False
    .AllowUserToOrderColumns = True
    .ReadOnly = True
    .SelectionMode = DataGridViewSelectionMode.FullRowSelect
    .MultiSelect = False
    .AllowUserToResizeRows = False
    .Rows(0).Selected = True ' highlight the first row
 End With

End sub

AddHandler myButton.Click, AddressOf check_for_highlighted_assessment

Public Sub check_for_highlighted_assessment()
  MessageBox.Show(data_grid.CurrentRow.Index.ToString)
End Sub

The first time, the button is clicked, I get "0", If the datagrid is repopulated with new data, then the next time the button is clicked I get an error.

Thanks for any help.

1
Your grid looks like it doesn't have any columns. And I get an error?. What is the error?LarsTech
The columns do get populated in the "add data to the columns" section. The error is "Object reference not set to an instance of an object." when trying to display the message box. And I can see in my error list that the value of data_grid.CurentRow is "nothing".John
I found that if I add .Rows(0).Cells(0).Selected = True, then it works without error, but that seems like a hack. I'd like it work without having to reference the cells as well.John
Yeah, but the problem is that you always need to have a "CurrentCell" in order for "CurrentRow" to return something.rory.ap

1 Answers

4
votes

In addition to setting the selected row after your data refreshes, you should set the current cell which will automatically set the CurrentRow property. So, for example:

.Rows(0).Selected
.CurrentCell = .Rows(0).Cells(0)