【问题标题】:Getting RowIndex based on the multiple selected cells on a DataGridGiew根据 DataGridGiew 上的多个选定单元格获取 RowIndex
【发布时间】:2015-10-23 18:27:53
【问题描述】:

我正在尝试根据 DataGridView 上的多个选定单元格获取行索引。如何在 VB.NET 中做到这一点?

这就是我所拥有的:

If e.RowIndex >= 0 Then
    Dim row As DataGridViewRow
    row = Me.DataGridView1.Rows(e.RowIndex)
    sno.Text = row.Cells("F2").Value.ToString
    sname.Text = row.Cells("F3").Value.ToString
    final.Text = row.Cells("F16").Value.ToString
End If

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    DatagridView 在每个单元格上都有一个 SelectedCells 属性和一个 RowIndex:

        For Each c As DataGridViewCell In DataGridView1.SelectedCells
            Debug.Print(c.RowIndex)
        Next
    

    您将获得每个选定单元格的命中 - 可能有重复的 rRowIndex。

    您可以将 SelectionMode 设置为行并使用 SelectedRows。


    要将第一个选定的单元格用作要使用的行:

    If DataGridView1.SelectedCells.count > 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(DataGridView1.SelectedCells(0).RowIndex)
        sno.Text = row.Cells("F2").Value.ToString
        sname.Text = row.Cells("F3").Value.ToString
        final.Text = row.Cells("F16").Value.ToString
    End If
    

    【讨论】:

    • 第二个选项可能不起作用 - 我认为 .Selected... 方法可能不会以定义的顺序返回单元格/行,即最后一个单元格可能是第 0 个单元格。
    猜你喜欢
    • 2013-06-14
    • 2013-06-20
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多