vb.net datagridview not showing selected row

John

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.

rory.ap

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Datagridview how to cast selected row to custom object

From Dev

Retrieving selected row in dataGridView as an object

From Dev

DataGridView- How to jump to the selected row in search?

From Dev

Delete a row in DataGridView Control in VB.NET

From Dev

VB.Net - DataGridView, previous entries get empty when creating new row

From Dev

Populate textboxes when clicking on a row in the DataGridView in VB?

From Dev

VB DataGridView 1 Row selection

From Dev

DataGridView selected row to display in text boxes

From Dev

Scroll to Datagridview selected Row

From Dev

How to add a UserControl to a DataGridView in VB.net, and have the control always showing?

From Dev

How to count the number of selected rows in datagridview vb.net

From Dev

Showing UIPickerView with selected row

From Dev

Disable individual cell highlighting but allowing full row selection in DataGridView with Vb.Net

From Dev

Retrieving selected row in dataGridView as an object

From Dev

print selected row from datagridview Error

From Dev

VB.Net - DataGridView, previous entries get empty when creating new row

From Dev

import csv file into datagridview with specific column and row in vb.net

From Dev

I can't import "row ids" XML nodes into DataGridView in VB.net

From Dev

Datagridview how to cast selected row to custom object

From Dev

Showing a single record in datagridview from access database vb.net

From Dev

Get Selected Row in DataGridView not working VB.Net

From Dev

Scroll to Datagridview selected Row

From Dev

How to check if have any row selected on DataGridView?

From Dev

VB.NET: Cannot display selected data from access database into datagridview

From Dev

Adding a record in Datagridview Row in Vb.net

From Dev

how to delete selected row from datagridview

From Dev

vb.net calculation in datagridview

From Dev

Vb.net Inserting a row to DataGridView deletes the row

From Dev

How to delete selected row from datagridview and database?

Related Related

  1. 1

    Datagridview how to cast selected row to custom object

  2. 2

    Retrieving selected row in dataGridView as an object

  3. 3

    DataGridView- How to jump to the selected row in search?

  4. 4

    Delete a row in DataGridView Control in VB.NET

  5. 5

    VB.Net - DataGridView, previous entries get empty when creating new row

  6. 6

    Populate textboxes when clicking on a row in the DataGridView in VB?

  7. 7

    VB DataGridView 1 Row selection

  8. 8

    DataGridView selected row to display in text boxes

  9. 9

    Scroll to Datagridview selected Row

  10. 10

    How to add a UserControl to a DataGridView in VB.net, and have the control always showing?

  11. 11

    How to count the number of selected rows in datagridview vb.net

  12. 12

    Showing UIPickerView with selected row

  13. 13

    Disable individual cell highlighting but allowing full row selection in DataGridView with Vb.Net

  14. 14

    Retrieving selected row in dataGridView as an object

  15. 15

    print selected row from datagridview Error

  16. 16

    VB.Net - DataGridView, previous entries get empty when creating new row

  17. 17

    import csv file into datagridview with specific column and row in vb.net

  18. 18

    I can't import "row ids" XML nodes into DataGridView in VB.net

  19. 19

    Datagridview how to cast selected row to custom object

  20. 20

    Showing a single record in datagridview from access database vb.net

  21. 21

    Get Selected Row in DataGridView not working VB.Net

  22. 22

    Scroll to Datagridview selected Row

  23. 23

    How to check if have any row selected on DataGridView?

  24. 24

    VB.NET: Cannot display selected data from access database into datagridview

  25. 25

    Adding a record in Datagridview Row in Vb.net

  26. 26

    how to delete selected row from datagridview

  27. 27

    vb.net calculation in datagridview

  28. 28

    Vb.net Inserting a row to DataGridView deletes the row

  29. 29

    How to delete selected row from datagridview and database?

HotTag

Archive