Results 1 to 6 of 6

Thread: [2008] Search a DataGridView

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    [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

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

    Re: [2008] Search a DataGridView

    vb.net Code:
    1. For Each row As DataGridViewRow In myDataGridView.Rows
    2.     row.Selected = (DirectCast(row.DataBoundItem, DataRowView)("InvoiceDate") = someDate)
    3. Next
    That will select every row where the date matches and unselect every row where it doesn't.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    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

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

    Re: [2008] Search a DataGridView

    First, here's a quick correction to the code in the last post:
    vb.net Code:
    1. For Each row As DataGridViewRow In myDataGridView.Rows
    2.     row.Selected = (CDate(DirectCast(row.DataBoundItem, DataRowView)("InvoiceDate")) = someDate)
    3. Next
    Now, to answer your other questions:
    vb.net Code:
    1. For Each row As DataGridViewRow In myDataGridView.Rows
    2.     With DirectCast(row.DataBoundItem, DataRowView)
    3.         row.Selected = (CDate(.Item("OrderDate")) = someDate AndAlso CInt(.Item("ProductID")) = someID)
    4.     End With
    5. Next
    vb.net Code:
    1. For Each row As DataGridViewRow In myDataGridView.Rows
    2.     row.Selected = (CStr(DirectCast(row.DataBoundItem, DataRowView)("FirstName")) LIKE "a*")
    3. Next

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    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.

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

    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
  •  



Click Here to Expand Forum to Full Width