|
-
May 3rd, 2007, 06:59 AM
#1
Thread Starter
Lively Member
[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:
Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bList As New System.ComponentModel.BindingList(Of ListItem)
Dim bSource As New BindingSource()
bSource.DataSource = bList
' ListItem has Name, ID and Important properties, which are String, Integer and Boolean
' respectively.
bList.Add(New ListItem("Item 1", 1, True))
bList.Add(New ListItem("Item 2", 2, False))
bList.Add(New ListItem("Item 3", 3, True))
bList.Add(New ListItem("Item 4", 4, False))
DataGridView1.DataSource = bSource
DataGridView1.ColumnHeadersVisible = False
' This makes sure that only 'Important' and 'Name' are shown and displayed
' in the correct order
DataGridView1.Columns(2).DisplayIndex = 0
DataGridView1.Columns(1).DisplayIndex = 1
DataGridView1.Columns(0).DisplayIndex = 2
DataGridView1.Columns(0).Visible = False
DataGridView1.Columns(2).Width = 20
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.GridColor = Color.White
DataGridView1.RowHeadersVisible = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bSource As BindingSource = DataGridView1.DataSource
bSource.Filter = "ID == 2"
DataGridView1.Refresh()
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.
-
May 3rd, 2007, 08:24 PM
#2
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.
-
May 3rd, 2007, 11:21 PM
#3
Thread Starter
Lively Member
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.
-
May 3rd, 2007, 11:29 PM
#4
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.
-
May 4th, 2007, 02:30 AM
#5
Thread Starter
Lively Member
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:
Private Sub TestDataView()
Dim table As DataTable = New DataTable("table")
table.Columns.Add(New DataColumn("item", Type.GetType("System.String")))
table.Columns.Add(New DataColumn("important", Type.GetType("System.Boolean")))
' Add five items.
Dim NewRow As DataRow
Dim i As Integer
For i = 0 To 4
NewRow = table.NewRow()
NewRow("item") = "Item " & i
NewRow("important") = False
table.Rows.Add(NewRow)
Next
table.Rows(0)("important") = True
table.AcceptChanges()
Dim bSource1 As New BindingSource
Dim bSource2 As New BindingSource
Dim dView1 As DataView = New DataView(table)
Dim dView2 As DataView = New DataView(table)
bSource1.DataSource = dView1
bSource2.DataSource = dView2
bSource1.Filter = ("important = True")
bSource2.Filter = ("important = False")
DataGridView1.DataSource = bSource1
DataGridView2.DataSource = bSource2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|