使用其值获取datagridview的rowindex和columnindex

时间:2013-09-19 11:00:00

标签: vb.net datagridview indexing

使用此代码,我可以找到我指定的单元格值的确切位置,

Dim value As String = "apple"
Dim _DisplayText_ As String = ""
Dim _row_ As Integer = 0 : Dim _col_ As Integer = 0
For i As Integer = 0 To Me.DataGridView.ColumnCount - 1
    For j As Integer = 0 To Me.DataGridView.RowCount - 1
        If Me.DataGridView.Rows(j).Cells(i).Value = value Then
            _DisplayText_ = Me.DataGridView.Rows(j).Cells(i).Tag
            _row_ = j
            _col_ = i
            Exit For
        End If
    Next
Next

有没有办法不使用这个嵌套循环?想象一下,如果datagridview的行是成千上万的数据......那就是soooo slooowww。

是否有与此类似的内置函数?

msgbox(dtg.findrow("apple"))
msgbox(dtg.findcol("apple"))

修改

我修改了循环并将其更改为

   Dim value As String = "apple"
   Dim _DisplayText_ As String = ""
   Dim _col_ As String = "colFruits"
   Dim _row_ As Integer = 0 : Dim _col_ As Integer = 0
   For i As Integer = 0 To Me.DataGridView.RowCount - 1
       If Me.DataGridView.Rows(j).Cells(_col_).Value = value Then
           _DisplayText_ = Me.DataGridView.Rows(j).Cells(_col_).Tag
           _row_ = j
           _col_ = i
           Exit For
      End If
   Next

0 个答案:

没有答案