[2008] Search a DataGridView
Hi
I have a datagridview that is bound to a datatable "Invoices" "Select * from Invoices".When the user presses on "Search Date"(Select * from Invoices where InvoiceDate=@Date) I want to highlight the rows in the datagridview that fit into the query.
I don't want to use a dataview,I want to display all the records of the table "Invoices" and the records returned by the search queries with highlight
Any help
Re: [2008] Search a DataGridView
vb.net Code:
For Each row As DataGridViewRow In myDataGridView.Rows
row.Selected = (DirectCast(row.DataBoundItem, DataRowView)("InvoiceDate") = someDate)
Next
That will select every row where the date matches and unselect every row where it doesn't.
Re: [2008] Search a DataGridView
thanks for your reply
one more question what if I want to select a row based on multiple conditions like OrderDate and productId or when i want to select a row (that has a field firstname) whose firstname begins with a(where FirstName like 'a%')
thanks
Re: [2008] Search a DataGridView
First, here's a quick correction to the code in the last post:
vb.net Code:
For Each row As DataGridViewRow In myDataGridView.Rows
row.Selected = (CDate(DirectCast(row.DataBoundItem, DataRowView)("InvoiceDate")) = someDate)
Next
Now, to answer your other questions:
vb.net Code:
For Each row As DataGridViewRow In myDataGridView.Rows
With DirectCast(row.DataBoundItem, DataRowView)
row.Selected = (CDate(.Item("OrderDate")) = someDate AndAlso CInt(.Item("ProductID")) = someID)
End With
Next
vb.net Code:
For Each row As DataGridViewRow In myDataGridView.Rows
row.Selected = (CStr(DirectCast(row.DataBoundItem, DataRowView)("FirstName")) LIKE "a*")
Next
Re: [2008] Search a DataGridView
I tried
Code:
For Each row As DataGridViewRow In myDataGridView.Rows
row.Selected = (CStr(DirectCast(row.DataBoundItem, DataRowView)("FirstName")) LIKE "a*")
Next
but i got the following error
Object reference not set to an instance of an object.
Re: [2008] Search a DataGridView
Ah, that's probably the last row causing the issue, which wouldn't actually be bound to anything. You can change that to use a For loop instead of a For Each loop and exclude the last row by the upper bound, or you can test the DataBoundItem first and ignore that row if it's Nothing.