使用 LINQ 查询的 DataGridView 单元格搜索 [英] DataGridView cell search using a LINQ query

查看:29
本文介绍了使用 LINQ 查询的 DataGridView 单元格搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm still relatively new to LINQ, and have done more stumbling around than anything, but I have really liked what I have seen so far. So with that in mind, I have a VB.NET search routine, part of which is provided, below, that checks all Text cells in a DataGridView for a given string (inclusive), using a basic set of nested loops to perform the search:

' 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).

Pretty straightforward. Now, my question is: is there a way to replicate this behavior using LINQ? Basically I would like the query to select (or return) the first DataGridViewCell whose text contains the search string. I've done some tinkering with sub-queries and the like, but I'm still having trouble wrapping my brain around the concepts (too many years of writing T-SQL, I guess).

显然嵌套循环工作正常,所以这更像是一种好奇心,真的.提前致谢!

Obviously the nested loop works fine, so this is more of a curiosity, really. Thanks in advance!

推荐答案

我能够成功使用这段代码:

I was able to use this code with some success:

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

...等等...

这篇关于使用 LINQ 查询的 DataGridView 单元格搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆