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.
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:
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()" ); } }
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:
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(); }
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.