0

我有一个datatable显示在DataGridView.

我希望用户在 中选择一个值,datagridview并将该值用作过滤器以在 中查找另一个值datatable

所以像:

SELECT col2 from DataTable2 where col1= 
(value in selected cell of the DataGridView)

编辑

好的,我添加了一些额外的信息,因为我不确定我问的是正确的问题:

我在下面有一个工具提示datagridview

Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles DataGridView1.CellFormatting

    If e.ColumnIndex = Me.DataGridView1.Columns("Last Stop").Index _
        AndAlso (e.Value IsNot Nothing) Then

        With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

            .ToolTipText = '(THIS IS WHERE I'M STUCK)

        End With

    End If

End Sub

我在上面停留的一点是我想在其中使用值Last Stop以便e.Value在我的 DataTable 中查找名称 -

ToolTipSN.Tables("DT_ToolTip")

4

1 回答 1

0

如果您想要严格比较,区分大小写和精确的单词:

Dim selectedCells = DataGridView1.SelectedCells.Cast(Of DataGridViewCell)()
If selectedCells.Any Then
    Dim filteredRows = From row In datatable2
       Join cell In selectedCells On row.Field(Of String)("col2") Equals cell.Value
       Select row
    ' either use For Each to enumerate the result:
    For Each row In filteredRows
        Dim col2 = row.Field(Of String)("col2")
        ' ...
    Next
    ' or use filteredRows.CopyToDataTable to create  a new DataTable from the result
End If

如果您想允许任何选定的单元格并且不区分大小写(没有连接效率较低):

Dim filteredRows = From row In datatable2
    Where selectedCells.Any(Function(c) StringComparer.OrdinalIgnoreCase.Equals(c.Value, row("col2")))

如果要允许任何选定的单元格,不区分大小写,并且部分单词需要匹配:

Dim filteredRows = From row In datatable2
    Where selectedCells.Any(Function(c) CStr(c.Value).
        IndexOf(row.Field(Of String)("col2"), StringComparison.OrdinalIgnoreCase) > -1)
于 2013-03-01T10:02:40.523 回答