Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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中搜索同一单元格_Vb.net_Datagridview - Fatal编程技术网

Vb.net 在不同的DataGridView中搜索同一单元格

Vb.net 在不同的DataGridView中搜索同一单元格,vb.net,datagridview,Vb.net,Datagridview,我有一段代码在第二个DataGridView中循环,试图匹配用户与之交互的主DataGridVew中的相同索引号: Private Sub AllegationsDataGridView_CellEnter(sender As Object, e As EventArgs) Handles AllegationsDataGridView.CellEnter Try Dim currentcolumn As DataGridViewColumn =

我有一段代码在第二个
DataGridView
中循环,试图匹配用户与之交互的主
DataGridVew
中的相同索引号:

Private Sub AllegationsDataGridView_CellEnter(sender As Object, e As EventArgs) Handles AllegationsDataGridView.CellEnter
        Try
            Dim currentcolumn As DataGridViewColumn =
        AllegationsDataGridView.Columns(AllegationsDataGridView.CurrentCell.ColumnIndex)
            For Each row As DataGridViewRow In parentgrid.Rows
                If row.Cells.Item(0).Value = AllegationsDataGridView.CurrentRow.Cells(0).Value Then
                    parentgrid.CurrentCell = parentgrid(0, row.Index)
                End If
            Next
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
endsub:

    End Sub

问题是数据源可能有成千上万个条目,我不希望它在找到匹配项之前循环遍历everysingle行。我想知道是否有更快的方法来解决这个问题?我在搜索所有数据时看到的唯一示例是对每一行使用
方法,或者使用
循环直到
方法,这仍然会出现相同的问题。

我建议您在使用数据源初始化datagridview时,构建一个字典,使行键对应(
AllegationsDataGridView.CurrentRow.Cells(0).Value
)中的行索引。使用数据源而不是数据网格来构建此字典


您将能够快速访问相应的行。

从注释:
它们都使用相同的数据源

我不确定用户不与之交互的DGV为什么需要保持
CurrentCell
同步,因为如果它们绑定到同一个数据源,会立即在B中看到对a的更改。尽管如此:

Private Sub dgv1_CellClick(sender As Object, 
               e As DataGridViewCellEventArgs) Handles dgv1.CellContentClick
    If dgv2.Rows.Count = 0 Then Exit Sub

    dgv2.CurrentCell = dgv2.Rows(e.RowIndex).Cells(e.ColumnIndex)
End Sub
这里,
dgv1
是用户网格,dgv2是另一个(“代码中的父项”)

如果它们共享一个DS,则不需要For/Each迭代,因为在每行和每列索引中都会有相同的数据。即使用户因为列索引保持不变而对列重新排序,它也会工作,只是内部的
DisplayIndex
发生了更改

这将使用
DataGridViewCellEventArgs
中的行和列索引来设置
CurrentCell
Exit子项
用于说明当一个可能有行而不是另一行时的启动

您可能需要处理要响应的事件。
CellContentClick
似乎最不有用:如果他们单击任何单元格空白,它不会启动。如果可以单击父/dgv2网格中的单元格,它也会崩溃。
cellnenter
如果他们直接单击dgv2,它也会崩溃


CellClick
似乎工作正常,但在它们同步之前会有很小的延迟。

您是手动填充DGV还是它们使用数据源?迭代行和单元格将非常繁琐和缓慢。它们都使用相同的数据源您是否试图在任何列或其他行中找到具有匹配值的单元格值e在那个专栏里?