Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 基于DataGridView上的选定单元格获取RowIndex_Vb.net_Visual Studio 2010_Datagridview - Fatal编程技术网

Vb.net 基于DataGridView上的选定单元格获取RowIndex

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

我正在尝试根据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.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)属性仍然是所选单元格所在行的索引?