|
-
Nov 25th, 2008, 03:13 AM
#1
Thread Starter
Junior Member
[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
-
Nov 25th, 2008, 04:34 AM
#2
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.
-
Nov 25th, 2008, 05:45 AM
#3
Thread Starter
Junior Member
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
-
Nov 25th, 2008, 05:57 AM
#4
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
-
Nov 25th, 2008, 08:47 AM
#5
Thread Starter
Junior Member
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.
-
Nov 25th, 2008, 05:23 PM
#6
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.
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
|