|
-
Nov 2nd, 2016, 06:02 PM
#1
Thread Starter
Lively Member
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()
-
Nov 2nd, 2016, 06:15 PM
#2
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:
Dim criteria = {Function(s As String) s.StartsWith("A"),
Function(s As String) s.EndsWith("z")}
Dim result1 = criteria.All(Function(f) f("Adam"))
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".
-
Nov 2nd, 2016, 06:30 PM
#3
Thread Starter
Lively Member
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
-
Nov 2nd, 2016, 07:33 PM
#4
Re: How do I add multiple Criteria selections usling a List(of Func(of DataRow, Boole
 Originally Posted by Christhemist
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|