vb.net 在 DataGridView 中获取行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1402944/
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-09 14:24:47  来源:igfitidea点击:

Getting row number in a DataGridView

vb.netdatagridviewfocus

提问by Cyclone

How do you get row number DataGridView cell? Specifically, if a user has selected a single cell, how can you get that row number? It needs to access a particular cell based on what the user has selected.

你如何获得行号 DataGridView 单元格?具体来说,如果用户选择了单个单元格,您如何获得该行号?它需要根据用户选择的内容访问特定的单元格。

I know that the RemoveAt method can be used to remove at the Focus, but you cannot get the row number at focus apparently?

我知道 RemoveAt 方法可用于在焦点处删除,但您显然无法获得焦点处的行号?

Thanks for the help!

谢谢您的帮助!

回答by Matt Hamilton

You can simply use RowIndexon the current cell:

您可以简单地在当前单元格上使用RowIndex

var row = dataGridView1.CurrentCell.RowIndex;

回答by Renjith.R

this one works fine .

这个很好用。

Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint

If e.RowIndex >= 0 Then
        Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
    End If
End Sub

回答by user11982798

If you in event procedure of datagridview you will found "e As DataGridViewCellEventArgs" you can get row number with e.RowIndex and column number with e.ColumnIndex

如果您在 datagridview 的事件过程中,您会发现“e As DataGridViewCellEventArgs”,您可以使用 e.RowIndex 获取行号,使用 e.ColumnIndex 获取列号

Private Sub myDGV_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles myDGV.CellLeave
    Dim myRow As Integer = e.RowIndex
End Sub

And if you in normal procedure you can get row number like this:

如果你在正常程序中你可以得到这样的行号:

Private Sub btnGetRow_Click(sender As Object, e As EventArgs) Handles btnGetRow.Click
    dim myRow1 as Integer = 0
    dim myRow2 as Integer = 0
    myRow1 = myDGV.CurrentCell.RowIndex
    myRow2 = myDGV.CurrentRow.Index
End Sub

回答by lenniep

It is nearly the same but you may also use this solution:

它几乎相同,但您也可以使用此解决方案:

var row = dataGridView1.CurrentRow.Index

回答by Rob Sherratt

Another way if you need to track user interaction with a DataGridView: In my case there is extra processing in some generic functions that use the Me.I_SelCol and Me.I_SelRow column and row coordinates, but I haven't shown that because it's not relevant to the OP.

如果您需要跟踪用户与 DataGridView 的交互,另一种方法是:在我的例子中,在一些使用 Me.I_SelCol 和 Me.I_SelRow 列和行坐标的通用函数中有额外的处理,但我没有显示,因为它不相关到 OP。

Best regards, Rob

最好的问候,罗布

Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged

        If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub

        ' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the 
        ' new cell as per user navigation using mouse or cursor keys.  We just need to store the current
        ' co-ordinates for the currently selected cell.

        Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
        Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y

Exit Sub