Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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和columnindex_Vb.net_Datagridview_Indexing - Fatal编程技术网

Vb.net 使用datagridview的值获取其rowindex和columnindex

Vb.net 使用datagridview的值获取其rowindex和columnindex,vb.net,datagridview,indexing,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 -

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

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的行是数千个数据……那就太慢了

是否有类似的内置功能

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

这种方法已经相当有效了。您可以通过将分析重点放在“您想要的特定区域”(例如,单元格所在的给定列)来改进它。通过依赖基于LINQ的方法,您可能会减少(一点)代码的大小,但它很可能会提供相当的性能(甚至更糟)。循环看起来杂乱无章,效率低下,但实际上恰恰相反。PS:您需要在外部循环中(在行>=0和列>0的条件内)进一步“退出”。您还应该将row/col的起始值设置为“无效”,如-1.PS2:为什么您认为遍历1000行会花费太长时间?(1000对一台计算机来说太多了?!)另外,你认为这些函数是如何工作的?他们需要检查所有的数据(如果有你想要的函数或依赖LINQ,无论如何都会涉及一种循环)。Varocarbas首先说了,但是是的,循环看起来比LINQ或使用哈希表更混乱,但实际上它们是。看看这个-我过去经常按它的列进行过滤,它非常好…但我不确定当数据数量相当大时它是否会很快…当数据库位于另一台计算机(带软件狗的桌面)并且我们使用的连接是wifi时,我在检索数据时会经历一个缓慢的过程。但是,它起作用了。谢谢