Link to home
Start Free TrialLog in
Avatar of deanlee17
deanlee17

asked on

Filtering a datagrid

Hi guys,

I need to filter a datagrid by one specific column. How can I do this in code? The grid is already loaded with data.

Thanks,
Dean.
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You can use a dataview to filter the results, but how have you filled the datagridview?
Avatar of deanlee17
deanlee17

ASKER

I have a dataset and im just filling the grid with....

Dim testobj = CountPartsDataSet.CounterfeitParts
        Me.flxCounterfeit.ItemsSource = testobj

Ideally I want the datagrid filtering as a user types into a textbox, how is this possible?
Something like this on TextChanged event:

        Dim dv As DataView = DirectCast(Me.DataGridView1.DataSource, DataView)
        dv.RowFilter = String.Format(" columnNameToFiler LIKE '%{0}%'", Me.txtFilterTextBox.Text)
Error:

Unable to cast object of type 'CounterfeitPartsDataTable' to type 'System.Data.DataView'.

My code looks like this....

 Dim dv As DataView = DirectCast(Me.flxCounterfeit.ItemsSource, DataView)
        dv.RowFilter = String.Format(" columnNameToFiler LIKE '%{0}%'", Me.txtFilterTextBox.Text)
Is it an WPF application?

If so try this instead http://stackoverflow.com/questions/4166403/wpf-datagrid-filter
Btw ive now added my own column name, so....

Dim dv As DataView = DirectCast(Me.flxCounterfeit.ItemsSource, DataView)
        dv.RowFilter = String.Format(" PartNumber LIKE '%{0}%'", Me.txtFilterTextBox.Text)


Same error
Yes its WPF, your example is in C#, i need it in VB
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CodeCruiser, you absolute legend.
@deanlee17,

You had to change only a small thing from the code I have posted and I didn't had any points for that !!!

I was testing the solution I provided (that is for Winforms) in WPF.

Thanks!
@jpaulino,

How was I to know you were testing the solution? When I said it was WPF I thought maybe you didnt know the solution.

Sorry about this.
You only had ".DefaultView" to the code I have provided !!!

Since you can split points I think it's a little unfair.
jpaulino, tell me exactly what you want me to do.
I don't need the points, that’s not the problem, but since you have the option of accept multiple answers you should do it and split the points from the persons that tried to help you (I have posted 95% of the code you need).

We don’t earn nothing with this, we just like to help and this (points) is a way to recognize it ;)
Yes i know about splitting points and do usually. I just didnt this time. Sorry about that, look out for my next question and we can sort something out ;)