Vb.net 基于DataGridView上的选定单元格获取RowIndex
我正在尝试根据DataGridView上选定的单元格获取行索引。如何在VB.NET中实现这一点 这就是我所拥有的:Vb.net 基于DataGridView上的选定单元格获取RowIndex,vb.net,visual-studio-2010,datagridview,Vb.net,Visual Studio 2010,Datagridview,我正在尝试根据DataGridView上选定的单元格获取行索引。如何在VB.NET中实现这一点 这就是我所拥有的: Dim iRowIndex As Integer For i = 0 To Me.grdTransaction.SelectedCells.Item(iRowIndex) iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString() Dim s As String = Me.grdTr
Dim iRowIndex As Integer
For i = 0 To Me.grdTransaction.SelectedCells.Item(iRowIndex)
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
Dim s As String = Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value
aList.Add(s)
MsgBox("Row index " & iRowIndex)
Next
你可以试试这个
Dim iRowIndex As Integer
Dim s As String
For i as Integer = 0 To Me.grdTransaction.SelectedCells.Count -1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
aList.Add(Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & format(iRowIndex))
Next
我想我不明白这个问题。为什么
iRowIndex = grdTransaction.SelectedRow.RowIndex
不行?多亏了@matzone,我才明白了这一点:
Dim iRowIndex As Integer
For i As Integer = 0 To Me.grdTransaction.SelectedCells.Count - 1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex
aList.Add(Me.grdTransaction.Rows(iRowIndex).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & Format(iRowIndex))
Next
DGV.CurrentRow.Index
即使在
selectionMode=CellSelect
的情况下也能工作,谢谢您的帮助。我得到了一个ArgumentsOutofRange例外,因为我选择的是单元格,而不是行Forgive me,因为我从来没有创建一个GridView,用户可以在其中选择单个单元格,但即使选择了特定单元格,也肯定是SelectedRow.RowIndex(或者更好的是SelectedRow)属性仍然是所选单元格所在行的索引?