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?
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.
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
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.
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
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.