Find substring in dataview/datagrid

btran007

Member
Joined
Aug 16, 2006
Messages
19
Programming Experience
Beginner
Hello everyone,

Can anyone please show me how to find substring in dataview/datagrid? Basically, a column contains bunch of phone numbers
410-555-1212
410-379-2215
410-443-5214
410-555-2351
410-555-5623
410-895-8951

After sorting-
410-379-2215
410-443-5214
410-555-1212
410-555-2351
410-555-5623
410-895-8951

I want to find all phone numbers starting string with like "410-555" and select the first occurence. I don't want to use RowFilter. I want to able to maintain the whole phone list. I have tried dv.Find("Like '410-555%'" and some other ways, but it doesn't work. I think my Find syntax is incorrect. Can anyone help me with this?

Thanks
btran
 
Here's a quick, but not efficient (if you have many many rows in your datagrid) solution.

VB.NET:
Public sub findDataGridRow(key as string)
        For Each dRow As DataGridViewRow In DataGrid1.Rows
            For Each cell As DataGridViewCell In dRow.Cells
                'String comparison goes here
                if key = cell.value.ToString '<--- Edit to fit your needs

                'Once you have found what you want then do:
                dRow.Selected = True

                Exit Sub 'Since you want the first found to be selected then exit the sub - makes it a bit faster...
            Next
        Next
End Sub
Since you are on .NET 2.0 you should be fine with the above ;), also keep in mind other - faster - solutions might be out there!
 
Last edited:
Thanks ABSOLUT,

This is a good idea and I have thought about it. But I have over 100K+ rows and 30 columns and that may take awhile. I want to minimize down to one column. I believe dv.Find() will work, but I don't know the exact syntax. If it comes down to it, I may have to resort to your solution.

btran
 
Back
Top