title: vb.net datagridview not showing selected row
body: <p>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:</p>
  
  <pre><code>Public sub show_data()
  
   ' Clear everything out of the data grid
   ' -------------------------------------
     With data_grid
       While data_grid.RowCount &gt; 0
         data_grid.Rows.Clear()
       End While
       While data_grid.ColumnCount &gt; 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
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks for any help.</p>