Results 1 to 5 of 5

Thread: [2005] Using a filter on BindingSource with BindingList

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    [2005] Using a filter on BindingSource with BindingList

    Hi there,

    I've been messing with the DataGridView component and it's really quite cool what you can do with it. I have it working for the most part, apart from one thing. I can't seem to get filtering working. The code I have is fort testing purposes only, basically just to get to know how to work with the DGV. I did notice that bSource.SupportsFiltering was set to False and I have no idea how to change that.

    vb Code:
    1. Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim bList As New System.ComponentModel.BindingList(Of ListItem)
    3.         Dim bSource As New BindingSource()
    4.  
    5.         bSource.DataSource = bList
    6.  
    7.         ' ListItem has Name, ID and Important properties, which are String, Integer and Boolean
    8.         ' respectively.
    9.  
    10.         bList.Add(New ListItem("Item 1", 1, True))
    11.         bList.Add(New ListItem("Item 2", 2, False))
    12.         bList.Add(New ListItem("Item 3", 3, True))
    13.         bList.Add(New ListItem("Item 4", 4, False))
    14.  
    15.         DataGridView1.DataSource = bSource
    16.  
    17.         DataGridView1.ColumnHeadersVisible = False
    18.  
    19.         ' This makes sure that only 'Important' and 'Name' are shown and displayed
    20.         ' in the correct order
    21.         DataGridView1.Columns(2).DisplayIndex = 0
    22.         DataGridView1.Columns(1).DisplayIndex = 1
    23.         DataGridView1.Columns(0).DisplayIndex = 2
    24.         DataGridView1.Columns(0).Visible = False
    25.  
    26.         DataGridView1.Columns(2).Width = 20
    27.         DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    28.         DataGridView1.GridColor = Color.White
    29.         DataGridView1.RowHeadersVisible = False
    30.         DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    31.  
    32.  
    33.     End Sub
    34.  
    35.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    36.         Dim bSource As BindingSource = DataGridView1.DataSource
    37.         bSource.Filter = "ID == 2"
    38.         DataGridView1.Refresh()
    39.     End Sub

    Basically, to test the filter, I wanted to press a button and then apply the filter. I am sure that once I get the hang of the filtering, I can implement it fully in my main app.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Using a filter on BindingSource with BindingList

    I just don't understand why, if something doesn't work, consulting the help is not the first thing that everyone does. The MSDN doco for that property says:
    Property Value
    true if the list is an IBindingListView and supports filtering; otherwise, false.

    Remarks
    If the list is not an IBindingListView, SupportsFiltering always returns false.
    Does the BindingList class implement the IBindingListView interface? The answer is NO. There are not many classes that do. BindingSource and DataView do, and I'm not aware of any others in the Framework, although there may be some. The fact that DataView does is the reason that you can bind and filter DataTables. If you want a list that supports filtering then you have to implement it yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Using a filter on BindingSource with BindingList

    Well, I did consult the MSDN docs and Googled like there was no tomorrow. I spent more than 4 hours trying to figure this out before even writing my OP. I still lack a great deal of understanding of how some things in VB work, especially when it comes to the nitty gritty stuff like that. I put the BindingList in a BindingSource, in the assumption that that would give me the filtering ability, as per the MSDN docs. I will take a look at the DataView class and see how I can use that.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Using a filter on BindingSource with BindingList

    The MSDN docs say that the list you bind to the BindingSource has to be an IBindingListView in order for filtering to be supported. The BindingList class does not implement the IBindingListView interface, so filtering is not supported.

    Also, you are not intended to use a BindingSource and a BindingList. The BindingList is intended to be bound directly to a control.

    The DataView class exists solely to provide a filtered and/or sorted view of a DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Using a filter on BindingSource with BindingList

    I went back to the proverbial drawing board and this is what I have now. It works exactly as I want it to. I have two BindingSources with their own DataView attached to two different DGVs presenting two different subsets of my data.
    vb Code:
    1. Private Sub TestDataView()
    2.         Dim table As DataTable = New DataTable("table")
    3.  
    4.         table.Columns.Add(New DataColumn("item", Type.GetType("System.String")))
    5.         table.Columns.Add(New DataColumn("important", Type.GetType("System.Boolean")))
    6.  
    7.         ' Add five items.
    8.         Dim NewRow As DataRow
    9.         Dim i As Integer
    10.         For i = 0 To 4
    11.  
    12.             NewRow = table.NewRow()
    13.             NewRow("item") = "Item " & i
    14.             NewRow("important") = False
    15.             table.Rows.Add(NewRow)
    16.         Next
    17.         table.Rows(0)("important") = True
    18.         table.AcceptChanges()
    19.  
    20.         Dim bSource1 As New BindingSource
    21.         Dim bSource2 As New BindingSource
    22.         Dim dView1 As DataView = New DataView(table)
    23.         Dim dView2 As DataView = New DataView(table)
    24.         bSource1.DataSource = dView1
    25.         bSource2.DataSource = dView2
    26.  
    27.         bSource1.Filter = ("important = True")
    28.         bSource2.Filter = ("important = False")
    29.  
    30.         DataGridView1.DataSource = bSource1
    31.         DataGridView2.DataSource = bSource2
    32.     End Sub
    Is this the Proper Way™ to do it? I noticed if I attached the table to the bSource1 and 2 directly, only the results of the last applied filter ("important = false") would be shown in both DGVs. Giving each BindingSource its own DataView solved this problem, that also seems to make more sense anyway.

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width