Friday, August 31, 2012

Bit fields as text in Infragistics UltraWinGrid

If you receive a bit field from database and want to show some text (Yes/No in this example), instead of checkboxes here is the solution. First you need a class that implements the Infragistics.Win.IEditorDataFilter interface. Here is my implementation:
  1. using System;
  2. using System.Windows.Forms;
  3. using Infragistics.Win;
  4.  
  5. namespace dbMotion.DAT {
  6. internal class GridCheckBoxDataFilter:IEditorDataFilter {
  7. #region IEditorDataFilter Members
  8.  
  9. object IEditorDataFilter.Convert(EditorDataFilterConvertArgs args) {
  10. switch (args.Direction) {
  11. case ConversionDirection.EditorToOwner:
  12. args.Handled = true;
  13. args.IsValid = true;
  14. CheckState state = (CheckState) args.Value;
  15. switch (state) {
  16. case CheckState.Checked:
  17. return "Yes";
  18. case CheckState.Unchecked:return "No";
  19. case CheckState.Indeterminate:return string.Empty;
  20. } break;
  21. case ConversionDirection.OwnerToEditor:args.Handled = true;
  22. args.IsValid = true;
  23. return args.Value.ToString() == bool.TrueString ? "Yes" : "No";
  24. case ConversionDirection.DisplayToEditor:args.Handled = true;
  25. args.IsValid = true;
  26. return args.Value.ToString() == "Yes";
  27. case ConversionDirection.EditorToDisplay:args.Handled = true;
  28. args.IsValid = true;
  29. return args.Value;
  30. } throw new Exception("Invalid value passed into GridChecBoxDataFilter.Convert()");
  31. }
  32.  
  33. #endregion
  34. }
  35. }
  36.  
And after this add the following code to the "InitializeLayout" event of your UltraWinGrid:
  1. private void Grid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
  2. {
  3. e.Layout.Bands[0].Columns["Col1"].Editor = new EditorWithText();
  4. e.Layout.Bands[0].Columns["Col1"].Style = ColumnStyle.FormattedText;
  5. e.Layout.Bands[0].Columns["Col1"].Editor.DataFilter = new GridCheckBoxDataFilter();
  6. }
  7. }
  8.