Results 1 to 4 of 4

Thread: How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boolean)?

  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boolean)?

    I am selecting records in a datagridview using a user input criteria with the code below. However, I am wanting to use multiple search criteria's as the user feels needed. The code I have works, it finds the qualifying records and turns them red, but when I search for another criteria it turns the previous records back to the color black.

    Code:
    Private Sub DataGridView1_RowPrePaint(sender As Object,
            e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
            If (e.RowIndex < 0 OrElse e.RowIndex = DataGridView1.NewRowIndex) Then Return
            Dim row = DataGridView1.Rows(e.RowIndex)
            If (String.IsNullOrEmpty(Filter)) Then
                row.DefaultCellStyle.ForeColor = Color.Black
            Else
                Dim data = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem,
                    DataRowView).Row
                If data.Field(Of String)(ColumnName).ToLower() _
                                                     .StartsWith(Filter.ToLower()) Then
                    row.DefaultCellStyle.ForeColor = Color.Red
                    DataGridView1(0, row.Index).Value = True
                Else
                    row.DefaultCellStyle.ForeColor = Color.Black
                End If
            End If
        End Sub
    And here is the search form code:

    Code:
    Sorter.Filter = ItemToFind
                Sorter.DataGridView1.Invalidate()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boole

    Firstly, not that "criteria" is already a plural. There's no such thing as "criterias". There is one criterion and many criteria.

    As for your question, criteria are simply Boolean expressions. How do you usually combine Boolean expressions? Using AND and/or OR operators, right? So, if you want to highlight rows that match two criteria then you would combine the two expressions with an AndAlso operator. If you want an arbitrary number of criteria then you can put them in an array or collection and then call All, e.g.
    vb.net Code:
    1. Dim criteria = {Function(s As String) s.StartsWith("A"),
    2.                 Function(s As String) s.EndsWith("z")}
    3.  
    4. Dim result1 = criteria.All(Function(f) f("Adam"))
    5. Dim result2 = criteria.All(Function(f) f("Aziz"))
    In that case, 'result1' will be False because not all the functions in the array return True when passed the value "Adam" while 'result2' will be True all functions do return True when fed "Aziz".

  3. #3

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boole

    How is this implemented into my current code? Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boole

    Quote Originally Posted by Christhemist View Post
    How is this implemented into my current code? Thanks
    However you decide to implement it when you take the time to think about it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width