vb.net - 使用 LINQ 查询的 DataGridView 单元格搜索

标签 vb.net linq datagridview subquery

我对 LINQ 还比较陌生,并且遇到的麻烦比任何事情都多,但我真的很喜欢到目前为止所看到的。因此,考虑到这一点,我有一个 VB.NET 搜索例程,下面提供了其中的一部分,它检查 DataGridView 中的所有文本单元格是否有给定的字符串(包括),使用一组基本的嵌套循环来执行搜索:

' Search for the first occurrence of the given string
For Each row As DataGridViewRow In dgvMembers.Rows
    ' Skip the new row
    If row.IsNewRow Then Exit For

    ' Loop through all the cells in the current row
    For Each cell As DataGridViewCell In row.Cells
        ' Skip non-text cells
        If cell.GetType IsNot GetType(DataGridViewTextBoxCell) Then Continue For

        ' Search for our matching text
        If cell.Value.ToString.ToUpper.Contains(searchText) Then
            ' Select the cell if we have a match
            dgvMembers.CurrentCell = cell
            WriteMessage("String '{0}' found.", searchText)
            Exit Sub
        End If
    Next
Next

' If we get to this point, we didn't find anything
WriteMessage("String '{0}' NOT found.", searchText)

非常简单。现在,我的问题是:有没有办法使用 LINQ 复制此行为?基本上我希望查询选择(或返回)第一个文本包含搜索字符串的 DataGridViewCell。我已经对子查询等进行了一些修改,但我仍然无法理解这些概念(我猜是写 T-SQL 太多年了)。

显然,嵌套循环工作得很好,所以这确实是一种好奇。提前致谢!

最佳答案

我能够使用此代码并取得一些成功:

Dim qry = From theRow as DataGridViewRow In dgvMembers.Rows, _
               theCell as DataGridViewCell In theRow.Cells _
          Where theCell.Value.ToString.ToUpper = searchText _
          Select theCell


Dim matchCell as DataGridViewCell = qry.First

dgvMembers.CurrentCell = matchCell

...等等...

关于vb.net - 使用 LINQ 查询的 DataGridView 单元格搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321116/

相关文章:

xml - 在 VB.NET 中使用 LINQ to XML 更新 XML 元素

c# - 在 UserControl 中居中标签

c# - "Grouping"字典值

c# - 在 C# datagridview 中隐藏某些列标题

vb.net - 更新 DataGridView 但输入 TabPage 时不选择任何行

.net - DataGridView 最后一列标题后的空白

asp.net - 如何阅读asp.net VB.net中提交的summernote文本区域中的文本

c# - 哪些文件应该从 C#/VB 中的 Visual Studio 项目文件 checkin SVN?

c# - 如何在 linq c# 中编写下面的 sql 查询,其中某些参数有时会为 null

c# - 使用 .Contains 的搜索功能包含一个字段中的术语