DataGridView cell search using a LINQ query

回眸只為那壹抹淺笑 提交于 2019-12-10 10:54:35

问题


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)

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!


回答1:


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

... etc...




回答2:


Posting a full code snippet using knslyr's response, but in the context of my original code, just for posterity:

Try
    ' Search (case insensitive) for the first occurrence of the given string
    Dim foundCell As DataGridViewCell = (
        From row As DataGridViewRow In dgvMembers.Rows,
        cell As DataGridViewCell In row.Cells
        Where cell.Value.ToString.ToUpper.Contains(searchText)
        Select cell
    ).First

    ' If we have a match, select it
    If foundCell IsNot Nothing Then
        ' Select the cell if we have a match
        dgvMembers.CurrentCell = foundCell
        WriteMessage("String '{0}' found.", searchText)
    Else
        ' No cell found.  Whoops.
        WriteMessage("String '{0}' NOT found.", searchText)
    End If
Catch ex As Exception
    ' An exception probably means the New row was reached.  So, no dice.
    WriteMessage("String '{0}' NOT found.", searchText)
End Try


来源:https://stackoverflow.com/questions/3321116/datagridview-cell-search-using-a-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!