Vb.net 从数据表中选择值
我有一个Vb.net 从数据表中选择值,vb.net,linq,datatable,Vb.net,Linq,Datatable,我有一个datatable,它显示在DataGridView中 我希望用户在datagridview中选择一个值,并将该值用作过滤器,以便在datatable中查找另一个值 比如: SELECT col2 from DataTable2 where col1= (value in selected cell of the DataGridView) 编辑 好的,我添加了一些额外的信息,因为我不确定我问的问题是否正确: 我在datagridview上有一个工具提示,如下所示: Sub data
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
我在上面遇到的问题是,我想使用最后一站
中的值来查找数据表中的名称-
工具提示n.表格(“DT_工具提示”)
如果需要严格比较,请区分大小写并使用精确的单词:
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) CStr(c.Value).
IndexOf(row.Field(Of String)("col2"), StringComparison.OrdinalIgnoreCase) > -1)
看看这不就是从DataGridView返回的值吗?我有这个值,我需要的是如何用这个值查询另一个数据表这有帮助吗?我补充了一些额外的信息,因为我不确定我问的问题是否正确
Dim filteredRows = From row In datatable2
Where selectedCells.Any(Function(c) CStr(c.Value).
IndexOf(row.Field(Of String)("col2"), StringComparison.OrdinalIgnoreCase) > -1)