PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
20120723 - i00 BindingList with DataGridView - with LINQ filtering support!-VBForums
Results 1 to 1 of 1

Thread: 20120723 - i00 BindingList with DataGridView - with LINQ filtering support!

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,336

    Lightbulb 20120723 - i00 BindingList with DataGridView - with LINQ filtering support!

    Download
    Anyone wishing to use this code in their projects may do so, however are required to leave a post on this thread stating that they are using this.
    A simple "I am using this in my project" will suffice.


    DO NOT MIRROR
    DO NOT LINK DIRECTLY TO THIS FILE - LINK TO THIS POST INSTEAD
    DO NOT RE-DISTRIBUTE THIS SOURCE CODE PARTLY OR IN ITS ENTIRETY

    20120723 - Download

    Total Downloads:

    Downloads per day:


    About

    This project was designed to provide a simple way to add data to a list that could be filtered and sorted, with ease... but (as with most of my projects) this quickly grew to support lots of other features such as full LINQ filtering, a datagrid view that supports UIEditors, filtering plugins, a BindingNavigator that has filtering options and more!



    Donations
    Donate Here - and be sure to put "i00 BindingList", your user name (or other alias), and if you want the amount/username disclosed in the description field

    Features

    BindingListView
    • Allows easy filtering
    • Allows easy sorting
    • Supports LINQ filtering


    DataGridView
    • UI Type Editor drawing on cells
    • UI Type Editor support for cell editing
    • Inbuilt column header menus for sorting and filtering options
    • Data filtering plugins
    • Highlighting of filtered text
    • Faster than the standard DGV
    • Images scale to fit cells
    • Images can be selected by double clicking on an image cell
    • Lists and allows Enum options to be selected with ease


    BindingNavigatorWithFilter
    • Adds filtering fields to the standard BindingNavigator


    Implementation

    To implement i00 BindingList into your project, first either:
    • Add the i00BindingList project to your solution and reference it (recommended)
    • Reference the i00BindingList.exe file that is output from this project
    • or you can bring all of *.vb files in the "i00BindingList" folder (from the zip) directly into your own project


    Creating a Binding List
    The Binding List can be created and filled just like any List(Of T):

    vb Code:
    1. 'Create a i00BindingList.BindingListView of the required object... in this case "Person"
    2. Dim blPersons As New i00BindingList.BindingListView(Of Person)
    3. '... and to fill it:
    4. Dim Person as New Person()
    5. 'fill person details here
    6. blPersons.Add(Person)

    Filtering a Binding List
    The Binding List can be filtered by taking advantage of i00BindingList.BindingListView.Filter property. The i00BindingList filters are in LINQ and support calls to functions from within your project itself. For example the following will filter our list to show all of the people born in 1982:

    vb Code:
    1. blPersons.Filter = "Year([DOB]) = 1982"

    To access a Public Shared Function (in this case PeopleFunctions.IsInFootballTeam()) in your project you can do so by going:

    vb Code:
    1. blPersons.Filter = GetType(PeopleFunctions).FullName & ".IsInFootballTeam([Me]) = True"

    Note: that while using filtering [Me], and other fields must be in square brackets.

    When filtering the Binding List the FilterChanged event is called.

    Binding List Binding
    The Binding List exposes a BindingSource Property that can be used as a DataSource for DataGridViews, or as a BindingSource for the BindingNavigator.

    The DataGridView
    The i00BindingList.DataGridView inherits from DataGridView and can be used on custom data sources to allow support for UIEditors. UIEditors are part of the Net Framework and allow custom editors for items in the i00 DataGridView automatically as long as the AllowUIEditorCells and DrawUIEditorCells properties of the grid are set to true.

    In the following you can see the ColorEditor in action:



    For more info on creating your own UIEditor's check this article.

    Filtering Plugins for the the DataGridView
    If a i00BindingList.DataGridView is bound to a i00BindingList.BindingListView it will automatically allow filtering from the header based on each column's data type. These can be extended to provided advanced filtering via plugins (as pictured below)



    Filter plugins are classes that implement i00BindingList.Plugins.iFilterPlugin; they automatically get picked up and allow the addition of extra filters based on each fields data type. Projects require no reference to the plugins (if they are in external files), however you will need to place them in the applications path.

    The download project includes three filters ColorFilter (in the i00BindingList project), DateFilter and RecentDateTimeFilter (in the Test project).

    Below is a list of implements and what they are used for after implementing iFilterPlugin:

    • ReadOnly Property DataTypes() As System.Type() - This should be set to return the data types (in an array) that we will be using this filter on.
    • ReadOnly Property Dispaly() As i00BindingList.Plugins.DisplayMethods - Sets when this filter will be displayed, options are DefaultHide, Always, DefaultShow
    • Sub LoadFromFilter(ByVal Filter As i00BindingList.AdvancedBindingSource.BasicFilterBase) - This is called when existing filter data (BasicFilterBase) is loaded and should update the controls in MenuItems accordingly.
    • Function MenuItems() As System.Collections.Generic.List(Of System.Windows.Forms.ToolStripItem) - Sets the list of controls that will be loaded into the menu for this filter.
    • Event UpdateFilter(ByVal sender As Object, ByVal e As i00BindingList.Plugins.UpdateFilterPluginEventArgs) - This should be raised when your controls cause the filter to be updated.


    Below is an example of an iFilterPlugin that is used to filter a date range:

    vb Code:
    1. Public Class DateFilter
    2.     Implements i00BindingList.Plugins.iFilterPlugin
    3.  
    4. #Region "Controls"
    5.     'This is a Series of controls that will be available through the DataGridView's filter menu
    6.  
    7.     'This is the check box that when checked filters the dates as selected by the MonthCalendar,
    8.     'This exists as the MonthCalendar has no way to have NO date selected...
    9.     Private WithEvents tsiSelectDate As New i00BindingList.PersistentToolStripMenuItem() With {.Text = "Select a date or date range:"}
    10.  
    11.     'This is the month calendar that allows the user to select a date period to filter by
    12.     Private WithEvents MonthCalendar As New MonthCalendar With {.MaxSelectionCount = Integer.MaxValue, .ShowTodayCircle = False}
    13.  
    14.     'Returns the controls back to the plugin that we want to add to the filter menu
    15.     Public Function MenuItems() As System.Collections.Generic.List(Of System.Windows.Forms.ToolStripItem) Implements i00BindingList.Plugins.iFilterPlugin.MenuItems
    16.         MenuItems = New List(Of ToolStripItem)
    17.         Static ToolStripControlHost As New ToolStripControlHost(MonthCalendar)
    18.         MenuItems.Add(tsiSelectDate)
    19.         MenuItems.Add(ToolStripControlHost)
    20.     End Function
    21.  
    22. #End Region
    23.  
    24. #Region "Data Specifications"
    25.  
    26.     'Returns an array of data types that we can use this filter on... in this case Date
    27.     Public ReadOnly Property DataTypes() As System.Type() Implements i00BindingList.Plugins.iFilterPlugin.DataTypes
    28.         Get
    29.             Return New System.Type() {GetType(Date)}
    30.         End Get
    31.     End Property
    32.  
    33.     'Specifies when this plugin will be displayed
    34.     'this is also, somewhat, controlled by the use of the i00BindingList.Plugins.ActiveFilterPlugins on the object property itself
    35.     Public ReadOnly Property Dispaly() As i00BindingList.Plugins.DisplayMethods Implements i00BindingList.Plugins.iFilterPlugin.Dispaly
    36.         Get
    37.             Return i00BindingList.Plugins.DisplayMethods.DefaultShow
    38.         End Get
    39.     End Property
    40.  
    41. #End Region
    42.  
    43. #Region "Events"
    44.  
    45.     'This is raised when our controls cause the filter to be updated...
    46.     'e.FilterBase is used to send back the BasicFilterBase that is created from the options selected from our controls
    47.     Public Event UpdateFilter(ByVal sender As Object, ByVal e As i00BindingList.Plugins.UpdateFilterPluginEventArgs) Implements i00BindingList.Plugins.iFilterPlugin.UpdateFilter
    48.  
    49. #End Region
    50.  
    51. #Region "BasicFilterBase"
    52.     'BasicFilterBase's basically allow the creation of "some-what automated" LINQ where statements, through the specification of certain parameters
    53.  
    54.     'This is a BasicFilter that will be used to create a LINQ where clause from based on data we will get from our controls
    55.     Public Class BasicDateFilter
    56.         Inherits i00BindingList.AdvancedBindingSource.BasicFilterBase
    57.  
    58.         'The data that we will be filtering on with our iFilterPlugin
    59.         Public DateFrom As Date
    60.         Public DateTo As Date
    61.  
    62.         'Generates a basic LINQ where statement from the prams above
    63.         Public Overrides ReadOnly Property GetLinq() As String
    64.             Get
    65.                 Return "CDate(Format(xItem.[" & Field & "], ""D"")) >= CDate(""" & DateFrom.ToString & """) AndAlso CDate(Format(xItem.[" & Field & "], ""D"")) <= CDate(""" & DateTo.ToString & """)"
    66.             End Get
    67.         End Property
    68.     End Class
    69.  
    70. #End Region
    71.  
    72. #Region "Filtering"
    73.  
    74.     'This is called when the filter needs to be re-loaded
    75.     'for example if the date 1/1/1 -> 2/2/2 is selected in a BasicDateFilter(Filter pram) we should update our controls to match this data
    76.     Public Sub LoadFromFilter(ByVal Filter As i00BindingList.AdvancedBindingSource.BasicFilterBase) Implements i00BindingList.Plugins.iFilterPlugin.LoadFromFilter
    77.         Dim BasicDateFilter = TryCast(Filter, BasicDateFilter)
    78.         FireMonthCalendar_DateChanged = False
    79.         If BasicDateFilter IsNot Nothing Then
    80.             'we have an existing BasicDateFilter so load the control properties from that
    81.             LastBasicDateFilter = BasicDateFilter
    82.             MonthCalendar.SelectionRange = New SelectionRange(BasicDateFilter.DateFrom, BasicDateFilter.DateTo)
    83.             tsiSelectDate.Checked = True
    84.         Else
    85.             'this field is not filtered (at least not with a BasicDateFilter), so load the default values...
    86.             LastBasicDateFilter = Nothing
    87.             tsiSelectDate.Checked = False
    88.         End If
    89.         FireMonthCalendar_DateChanged = True
    90.     End Sub
    91.  
    92.     Dim FireMonthCalendar_DateChanged As Boolean = True
    93.     Dim LastBasicDateFilter As BasicDateFilter
    94.  
    95.     'When our MonthCalendar is updated raise the UpdateFilter event so that the Plugin can re-filter the data
    96.     Private Sub MonthCalendar_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar.DateChanged, MonthCalendar.DateSelected
    97.         If FireMonthCalendar_DateChanged Then
    98.             Dim BasicDateFilter = New BasicDateFilter With {.DateFrom = MonthCalendar.SelectionRange.Start, .DateTo = MonthCalendar.SelectionRange.End}
    99.             If LastBasicDateFilter Is Nothing OrElse (BasicDateFilter.DateFrom <> LastBasicDateFilter.DateFrom OrElse BasicDateFilter.DateTo <> LastBasicDateFilter.DateTo) Then
    100.                 RaiseEvent UpdateFilter(Me, New i00BindingList.Plugins.UpdateFilterPluginEventArgs() With {.FilterBase = BasicDateFilter})
    101.                 LastBasicDateFilter = BasicDateFilter
    102.                 tsiSelectDate.Checked = True
    103.             End If
    104.         End If
    105.     End Sub
    106.  
    107.     'When the "Select a date or date range" option is checked call the MonthCalendar_DateChanged so that the Plugin can re-filter the data
    108.     Private Sub tsiSelectDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsiSelectDate.Click
    109.         tsiSelectDate.Checked = Not tsiSelectDate.Checked
    110.         LastBasicDateFilter = Nothing
    111.         If tsiSelectDate.Checked = True Then
    112.             'filter
    113.             MonthCalendar_DateChanged(MonthCalendar, New DateRangeEventArgs(MonthCalendar.SelectionRange.Start, MonthCalendar.SelectionRange.End))
    114.         Else
    115.             'unfilter
    116.             RaiseEvent UpdateFilter(Me, New i00BindingList.Plugins.UpdateFilterPluginEventArgs())
    117.         End If
    118.     End Sub
    119.  
    120. #End Region
    121.  
    122. End Class

    The above code will produce the following DataGridView filter:



    Change Log
    20120723
    • Fixed a bug that when canceling an edit (eg. in a DGV) on an existing row on the Binding List would delete the item!

    20120719
    • Made properties / functions etc of the BindingListView use T instead of object .. eg: Item(ByVal Index As Integer) As T
    • AddRange added to BindingListView
    • Added AddingFilterPlugin event to DataGridView that allows programmers to cancel the plugin from being loaded, or modify plugin settings
    • Fixed a bug where the custom filter plugins would filter the data multiple times instead of just once
    • Changed Filter Linq query structure to remove requirement for "From xItem in ..."
    • Changed Filter Linq query structure so that you can just specify the fields with "[field]"


    Thanks
    Thanks for downloading... Also please provide feedback, rate this thread and say thanks if this helped you

    Suggestions on possible improvements are much appreciated

    Also I extend a special thanks to the users who thanked / rated this post.

    Thanks again
    Kris

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width