Results 1 to 6 of 6

Thread: How to find a DataGridViewRow matching a datarow?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    110

    How to find a DataGridViewRow matching a datarow?

    If the DataGridView is binded to a BindingSource, whic is in turn binded to a DataTable.

    Now I need to set a row in DataGridView as be selected. I know how to do this if I get the DataGridViewRow and set its selected property as True.

    But I need to pick one or several rows matching some filter criteria, and set as selected. Because DataGridView does not provide select function, I have to use the DataTable.Select to retrieve the right rows I want.

    Here comes the problem, after I get this DataRow(), how can I set them selected in DataGridView?
    Last edited by sunhpj; Jul 26th, 2008 at 06:11 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    110

    Re: How to find a DataGridViewRow matching a datarow?

    I posted this thread for couple of days but not replied yet.

    Is it rather impossible, without naive looping the manually find it.
    Last edited by sunhpj; Jul 28th, 2008 at 12:35 AM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to find a DataGridViewRow matching a datarow?

    vb.net Code:
    1. Dim rows As DataRow() = myDataTable.Select(...)
    2.  
    3. For Each gridRow As DataGridViewRow In myDataGridView.Rows
    4.     If Array.IndexOf(rows, DirectCast(gridRow.DataBoundItem, DataRowView).Row) <> -1 Then
    5.         'Select the row.
    6.     End If
    7. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    110

    Re: How to find a DataGridViewRow matching a datarow?

    Thanks for reply.
    I believe it works. though still use looping,
    I prefer some property to get this,but it seems not exist.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    110

    Re: How to find a DataGridViewRow matching a datarow?

    For simple selection it works.

    As for using Select

    But for some complicated condition, how can I do this.

    For example, My Grid has a column named "A B C" (blank between characters)

    I need to define

    table.Select ( " Substring ( '" & columnname & " ' , 0,8 ) = Substring ( ' " & value & "' , 0,8)")

    but it always return emply rows, thought infact should not

    If I loop myself, it becomes rather slow
    Last edited by sunhpj; Jul 28th, 2008 at 02:04 AM.

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

    Re: How to find a DataGridViewRow matching a datarow?

    Have you actually looked at the string you're passing to Select? I'll wager not, because it is NOT what you think it is, or at least it's not what it should be. Instead of what you have there do this:
    vb.net Code:
    1. Dim str As String = " Substring ( '" & columnname & " ' , 0,8 ) = Substring ( ' " & value & "' , 0,8)"
    2.  
    3. MessageBox.Show(str)
    4. table.Select (str)
    It's surprising what you can see when you actually look at your data, rather than just assuming it's correct.

    Also, if you use String.Format instead of lots of string concatenation your code becomes more readable, so errors like this are less likely.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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