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.
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
deanlee17

8/22/2022 - Mon
Jorge Paulino

You can use a dataview to filter the results, but how have you filled the datagridview?
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?
Jorge Paulino

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)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
deanlee17

ASKER
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)
Jorge Paulino

Is it an WPF application?

If so try this instead http://stackoverflow.com/questions/4166403/wpf-datagrid-filter
deanlee17

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
deanlee17

ASKER
Yes its WPF, your example is in C#, i need it in VB
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
deanlee17

ASKER
CodeCruiser, you absolute legend.
Jorge Paulino

@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!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
deanlee17

ASKER
@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.
Jorge Paulino

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

Since you can split points I think it's a little unfair.
deanlee17

ASKER
jpaulino, tell me exactly what you want me to do.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jorge Paulino

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 ;)
deanlee17

ASKER
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 ;)