Filtering Unbound DataGridView

Hi Experts,

I have an unbound DataGridView based on this code.

How can I filter the data, e.g. using a filter something like "Product Name" is not empty?

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
You will have to loop through the rows.

In my opinion, a better approach is to bind the grid to an inmemory datatable instead of unbound.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adriankohwsCommented:
It is inefficient, filter using where clause to get your dataset with only those that [Product Name] is not empty instead.
0
ArkCommented:
Actually, it can be done
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rows = (From r In DataGridView1.Rows Where
                    CType(r, DataGridViewRow).Cells("Product Name").
                    Value <> "").Cast(Of DataGridViewRow)().ToArray
        DataGridView1.Rows.Clear()
        DataGridView1.Rows.AddRange(rows)
    End Sub
'or

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim rows = (From r In DataGridView1.Rows Where
                    CType(r, DataGridViewRow).Cells("Product Name").
                    Value = "").Cast(Of DataGridViewRow)().ToArray
        For Each r In rows
            If Not r.IsNewRow Then DataGridView1.Rows.Remove(r)
        Next
    End Sub

Open in new window

But it's not 'filtering' - it's 'repopulating' (sample1) or 'editing' (sample 2). Unlike filtering, you can not restore data (remove filter) in both cases - all data with empty Product Name cells will be lost. I agree with other experts - use in-memory datatable/list/dictionary or whatever enumerable to store data and bind it to datagridview.
0
ArkCommented:
Here is example of using in-memory List(of T) instead of ubound dataGridView:
Public Class Form1
    Private Products As List(Of Product)
    Private bsProducts As BindingSource

    'Populate List(of T) and bind it to dgv
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Products = New List(Of Product)
        For i = 1 To 4
            Products.Add(New Product With {
                .Product_ID = i,
                .Product_Name = "Product " & i,
                .Product_Price = i * 1000,
                .Choise = If(i Mod 2 = 0, "True", "False")})
        Next
        DataGridView1.DataSource=nothing
        bsProducts = New BindingSource(Products, Nothing)
        DataGridView1.DataSource = bsProducts
        'Rename column headers
        For Each c As DataGridViewColumn In DataGridView1.Columns
            c.HeaderText = c.HeaderText.Replace("_", " ")
        Next
        'Replace autogenerated Text column with combo
        DataGridView1.Columns.Remove("Choise")
        Dim cmb As New DataGridViewComboBoxColumn()
        cmb.HeaderText = "Select Data"
        cmb.Name = "cmb"
        cmb.MaxDropDownItems = 4
        cmb.Items.Add("True")
        cmb.Items.Add("False")
        cmb.DataPropertyName = "Choise"
        DataGridView1.Columns.Add(cmb)
    End Sub

    'Apply filter on Product_Price > 2000, for example
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        bsProducts.DataSource = (From p In Products Where p.Product_Price > 2000).ToList
    End Sub
    'Remove filter
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        bsProducts.DataSource = Products
    End Sub
End Class
'Class representing record
Public Class Product
    Public Property Product_ID As Integer
    Public Property Product_Name As String
    Public Property Product_Price As Integer
    Public Property Choise As String
End Class

Open in new window

0
Ed70Commented:
Here is a good summary of various approaches to the problem of DataGridView filtering with accompanying pros and cons:
http://10tec.com/articles/datagridview-filter.aspx
After reading the article, I also vote for an inmemory underlying data source like DataTable. You can use DataTable.DefaultView.RowFilter to filter your "unbound" data easily and fast.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.