Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView Filtering Using BindingSource (Of CustomClassObjects)

I want to filter through my DataGridView data. My DataGridView's DataSource is bound to a BindingSource. The BindingSource contains a list of objects from my clsBillHeader class.

Here is the first bit of code:

Dim bSource As New BindingSource
bSource.DataSource = clsBillHeader.GetAll()
dgvBills.DataSource = bSource
bSource.Filter = "JobNumber Like '100%'" //Filter doesn't actually work

GetAll()

Public Shared Function GetAll() As List(Of clsBillHeader)
    Dim mycn As New SqlConnection(connection)
    Dim mycmd As New SqlCommand("SELECT * FROM Headers", mycn)
    mycn.Open()
    Dim myreader As SqlDataReader = mycmd.ExecuteReader
    Dim myList As New List(Of clsBillHeader)
    While myreader.Read
        Dim item As New clsBillHeader()
        SetByReader(myreader, item) //Sets all values correctly (such as forein keys)
        myList.Add(item)
    End While
    mycn.Close()
    Return myList
End Function

This successfully returns all my values needed as you can see in this screenshot:

BindingSource


The problem is it doesn't filter through anything ... my JobNumber Like '100%' doesn't seem to be filtering at all. As seen below:

Unwanted Results

I should just be getting the first two numbers but it returns everything else ...

Why am I not using DataView to use filtering?

Well, DataView would cause me to use a DataTable that is directly bound to the SQL table. Some values in my table are foreign keys and would need to be transformed into something more meaningful than integers.

Ex: FK_Author = 1 would be the value in the DataTable. Instead, I use my SetByReader to transform it into Author = "Alex". That's why I want to use a BindingSource.

All I need is to filter through a DataGridView bound to a class of clsBillHeaders. Has anyone had the same problem?

EDIT

Take a look at this screenshot. Apparently it doesn't support Filtering ...

NoFilter Support

Why does my BindingSource variable have this by default?

like image 360
Alex Avatar asked Aug 15 '13 14:08

Alex


2 Answers

Ah, I understand the problem now. Actually your datasource doesn't support Filtering. BindingSource.Filter will work only when your datasource implements IBindingListView

Only underlying lists that implement the IBindingListView interface support filtering.

So, to make it work you've to change your underlying datasource. If you don't wanna use DataView for some reason try using BindingSource itself as a datasource.

Edit: May this links help how to support filtering http://blogs.msdn.com/b/winformsue/archive/2008/05/19/implementing-filtering-on-the-ibindinglistview.aspx and http://blogs.msdn.com/b/winformsue/archive/2007/12/07/implementing-the-ibindinglistview-for-filtering.aspx

Hope this helps

like image 94
Sriram Sakthivel Avatar answered Sep 21 '22 16:09

Sriram Sakthivel


I found a quick and simple solution, I just convert the List to a DataTable, here is the link How to fill a datatable with List<T>

like image 31
Jervie Vitriolo Avatar answered Sep 18 '22 16:09

Jervie Vitriolo