I'm having trouble getting a filter to work on a BindingSource that is the DataSource for a DataGridView control. Basically, I have LINQ query that is the DataSource for the BindingSource and I would like to filter down the results. Below is an example of what I'm trying to accomplish.
Dim query = From row In dataTable _
Select New MyRow(row)
Dim bs As New BindingSource()
bs.DataSource = query.ToList()
grid.DataSource = bs
bs.Filter = "Col1 = 'value'"
...
Public Class MyRow
Private _key As String
Private _col1 As String
Public Sub New(ByVal row As DataTableRow)
_key = GetNewKeyValue()
_col1 = row.Col1
End Sub
Public ReadOnly Property Key() As String
Get
Return _key
End Get
End Property
Public ReadOnly Property Col1() As String
Get
Return _col1
End Get
End Property
End Class
So, I can see all the rows in the DataGridView control but the filter doesn't have any effect. If I switch the BindingSource's DataSource to use a DataTable, then the filtering works as expected. What am I missing?
Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21.
Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.
The expression in the Where clause is used to determine which data rows will be included in the DataView, and is the basis for the filter. Expression-based filters offer more powerful and complex filtering than the simpler string-based filters. The string-based and expression-based filters are mutually exclusive.
From the BindingSource documentation:
Typically used in complex data-binding scenarios, the Filter property allows you to view a subset of the DataSource. Only underlying lists that implement the IBindingListView interface support filtering
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With