【问题标题】:Getting RowIndex based on the selected cells on a DataGridView根据 DataGridView 上的选定单元格获取 RowIndex
【发布时间】:2013-06-14 17:05:11
【问题描述】:

我正在尝试根据我在 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

【问题讨论】:

    标签: vb.net visual-studio-2010 datagridview


    【解决方案1】:

    感谢@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
    

    【讨论】:

      【解决方案2】:

      DGV.CurrentRow.Index

      即使selectionMode = CellSelect也可以工作

      【讨论】:

        【解决方案3】:

        我认为我没有理解这个问题。为什么

        iRowIndex = grdTransaction.SelectedRow.RowIndex
        

        不工作?

        【讨论】:

        • 因为我选择的是单元格,而不是行
        • 请原谅我,因为我从来没有创建过一个用户可以选择单个单元格的 GridView,但是即使选择了一个特定的单元格,那么 SelectedRow.RowIndex (或者更好的是,SelectedRow)属性肯定是仍然是所选单元格所在行的索引?
        【解决方案4】:

        你可以试试这个..

        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
        

        【讨论】:

        • 感谢您的光临。我收到 ArgumentsOutofRange 异常
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        相关资源
        最近更新 更多