|
-
Jul 26th, 2008, 05:24 AM
#1
Thread Starter
Lively Member
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.
-
Jul 28th, 2008, 12:17 AM
#2
Thread Starter
Lively Member
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.
-
Jul 28th, 2008, 12:35 AM
#3
Re: How to find a DataGridViewRow matching a datarow?
vb.net Code:
Dim rows As DataRow() = myDataTable.Select(...) For Each gridRow As DataGridViewRow In myDataGridView.Rows If Array.IndexOf(rows, DirectCast(gridRow.DataBoundItem, DataRowView).Row) <> -1 Then 'Select the row. End If Next
-
Jul 28th, 2008, 01:19 AM
#4
Thread Starter
Lively Member
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.
-
Jul 28th, 2008, 01:57 AM
#5
Thread Starter
Lively Member
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.
-
Jul 28th, 2008, 02:06 AM
#6
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:
Dim str As String = " Substring ( '" & columnname & " ' , 0,8 ) = Substring ( ' " & value & "' , 0,8)" MessageBox.Show(str) 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.
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
|