簡體   English   中英

從數據表中選擇值

[英]SELECT value from DataTable

我有一個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

我在上面e.Value是我想使用Last Stop的值,以便e.Value在我的DataTable中查找名稱-

ToolTipSN.Tables(“ DT_ToolTip”)

如果要嚴格比較,請使用區分大小寫和精確的單詞:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM