vb.net datagridview 未显示选定的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19550634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:35:47  来源:igfitidea点击:

vb.net datagridview not showing selected row

vb.netdatagridview

提问by 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:

我第一次填充 datagridview 时,它会突出显示第一行,并且我有一个按钮,单击该按钮时会显示所单击的索引值,以便我知道单击了哪一行。如果用户再次搜索,我会清除 datagridview 的内容,然后用新数据重新填充它。同样,第一行 (index(0)) 被突出显示,但是这一次,当用户单击按钮时,即使网格的第一行被突出显示,我也会收到一个错误,显示 data_grid.rows 什么都没有。我试过将第一行设置为 .selected = true,但这不会改变任何东西。它只在第一次有效,然后就再也无效了。这是我的代码:

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.

第一次单击按钮时,我得到“0”,如果数据网格重新填充了新数据,则下次单击该按钮时出现错误。

Thanks for any help.

谢谢你的帮助。

回答by rory.ap

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

除了在数据刷新后设置所选行之外,您还应该设置将自动设置CurrentRow属性的当前单元格。因此,例如:

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