• North American Sales: 1-800-231-8588
  • Global Contacts
  • My Account
Infragistics Infragistics
Menu
  • North American Sales: 1-800-321-8588
  • My Account
    • Sign In/Register
  • Design & DevelopmentDesign & Develop
    • Best Value
      Infragistics Ultimate The complete toolkit for building high performing web, mobile and desktop apps.
      Indigo.Design Use a unified platform for visual design, UX prototyping, code generation and application development.
    • Web
      Ignite UI for Angular Ignite UI for JavaScript Ignite UI for React Ultimate UI for ASP.NET Indigo.Design
    • Desktop
      Ultimate UI for Windows Forms Ultimate UI for WPF
      Prototyping
      Indigo.Design
    • Mobile
      Ultimate UI for Xamarin Ultimate UI for iOS Ultimate UI for Android
    • Automated Testing Tools
      Test Automation for Micro Focus UFT: Windows Forms Test Automation for Micro Focus UFT: WPF Test Automation for IBM RFT: Windows Forms
  • UX
    • Indigo.Design Desktop Collaborative prototyping and remote usability testing for UX & usability professionals
    • Indigo.Design A Unified Platform for Visual Design, UX Prototyping, Code Generation, and App Development
  • Business Intelligence
    • Reveal Embedded Accelerate your time to market with powerful, beautiful dashboards into your apps
    • Reveal App Empower everyone in your organization to use data to make smarter business decisions
  • Team Productivity
  • Learn & Support Support
    • Help & Support Documents
    • Blogs
    • Forums
    • Product Ideas
    • Reference Applications
    • Customer Stories
    • Webinars
    • eBook & Whitepapers
    • Events
  • Free Trials
  • Pricing
    • Product Pricing / Buy Online
    • Renew Existing License
    • Contact Us
Windows Forms
  • Product Platforms
  • More
Windows Forms
Windows Forms Using an WinGrid DataFilter to Convert Strings to Bools (and vice-versa) for a Checkbox Column
  • Blog
  • Files
  • Wiki
  • Members
  • Mentions
  • Tags
  • More
  • Cancel
  • New
Windows Forms requires membership for participation - click to join
  • Windows Forms
  • Best Practices for Placing a DropDown or Combo in an WinGrid Cell
  • Change Color of Column Headers in WinGrid
  • Disable WinGrid Row, Cell or Column with Activation Object
  • Performance in the WinTilePanel
  • Select All Rows in WinGrid Programatically
  • UI Thread Marshaling in the Model Layer
  • Using an WinGrid DataFilter to Convert Strings to Bools (and vice-versa) for a Checkbox Column

Using an WinGrid DataFilter to Convert Strings to Bools (and vice-versa) for a Checkbox Column

Introduction

The DataFilter in the WinGrid provides a powerful and flexible means for transforming data from one format to another when putting data into or extracting it from the grid. This article will demonstrate data filtering by showing an  WinGrid with a column of checkboxes which have an underlying ADO.NET DataColumn (from which the values of the cells in that column originate) containing strings, not bools. The strings are either "YES" or "NO". Without the assistance of data filters, converting the strings to bools ("YES" to true, "NO" to false) and vice-versa would be tedious, at best. We will explore how using a filter between the cell and the checkbox on the cell can simplify this process.

Step-By-Step Example

First, make a class that implements the Infragistics.Win.IEditorDataFilter interface. This interface requires that your class implements only one method: the Convert method. Convert() takes in a parameter of type Infragistics.Win.EditorDataFilterConvertArgs and returns a System.Object. We will be checking the ConversionDirection of the EditorDataFilterConvertArgs argument to see if the data is coming from a checkbox to a cell or vice-versa. If the data is coming from a checkbox that means that we need to convert the CheckState of the checkbox to a string ("YES", "NO", or the empty string). If the data value is being pushed into the checkbox from the cell, we need to convert a string to a CheckState. Here is the code that does this:

C#

class CheckEditorDataFilter : Infragistics.Win.IEditorDataFilter
{
    object Infragistics.Win.IEditorDataFilter.Convert( Infragistics.Win.EditorDataFilterConvertArgs args )
    {
        switch( args.Direction )
        {
            case ConversionDirection.EditorToOwner:
                args.Handled = true;
                CheckState state = (CheckState)args.Value;
                switch( state )
                {
                    case CheckState.Checked:
                        return "YES";
                    case CheckState.Unchecked:
                        return "NO";
                    case CheckState.Indeterminate:
                        return String.Empty;
                }
                break;
            case ConversionDirection.OwnerToEditor:
                args.Handled = true;
                if( args.Value.ToString() == "YES" )
                    return CheckState.Checked;
                else if( args.Value.ToString() == "NO" )
                    return CheckState.Unchecked;
                else
                    return CheckState.Indeterminate;
        }
        throw new Exception( "Invalid value passed into CheckEditorDataFilter.Convert()" );
    }
}

VB

Public Class CheckEditorDataFilter 
Implements Infragistics.Win.IEditorDataFilter
    Public Function Convert(ByVal args As Infragistics.Win.EditorDataFilterConvertArgs)As Object Implements Infragistics.Win.IEditorDataFilter.Convert
        Select Case args.Direction
            Case ConversionDirection.EditorToOwner
                args.Handled = True
                Select Case CType(args.Value, CheckState)
                    Case CheckState.Checked
                        Return "YES"
                    Case CheckState.Unchecked
                        Return "NO"
                    Case CheckState.Indeterminate
                        Return String.Empty
                End Select
            Case ConversionDirection.OwnerToEditor
                args.Handled = True
                If args.Value = "YES" Then
                    Return CheckState.Checked
                ElseIf args.Value = "NO" Then
                    Return CheckState.Unchecked
                Else
                    Return CheckState.Indeterminate
                End If
        End Select
    End Function
End Class

Once that class is written, you need to set the DataFilter of the checkbox to a new instance of CheckEditorDataFilter:

C#

private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
    e.Layout.Bands[0].Columns[0].Style = ColumnStyle.CheckBox;
    e.Layout.Bands[0].Columns[0].Editor.DataFilter = new CheckEditorDataFilter();
}

VB

Private Sub UltraGrid1_InitializeLayout(ByVal sender As System.Object, _
       ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) _
       Handles UltraGrid1.InitializeLayout
    e.Layout.Bands(0).Columns(0).Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox
    e.Layout.Bands(0).Columns(0).Editor.DataFilter = New CheckEditorDataFilter 
End Sub

Please note that the WinGrid uses the same editor for all columns of a single type. As such, if you have multiple Boolean columns within a single grid and only want the DataFilter to apply to a single column, you should use an UltraCheckEditor as the Column's EditorControl and specify the DataFilter on the UltraCheckEditor rather than the Column's Editor.

  • WinGrid
  • datafilter
  • Share
  • History
  • More
  • Cancel
Related
Recommended