C1FlexGrid ComboBox Editor Using a Custom TypeConverter

TypeConverters are an in-built .NET mechanism for converting objects of one type to another (for instance from an enum value to a string value). This blog explains how one can apply the same concept to C1Flexgrid's ComboBox Editor, i.e display strings for each enum value contained in the ComboBox within C1FlexGrid.

FlexGrid Containing an Enum Column

In C1FlexGrid, if the column's DataType property is set to an enumeration, the grid will automatically build and use a data map with the names of each value in the enumeration. You may refer to this Documentation link to know more about this. The enum used in this solution is called “MyGreatEnum”. The display strings for each enum value are found in a resx file “Resources.MyGreaEnum.resx”. Here we define a custom TypeConverter class (MyGreatEnumConverter) that converts enum values to and from string values using localized strings from the project resources. (It uses the code from the sample “Localizing .NET Enums” to localize enum values.) Setting the column's datatype to enum shows all enum values the FlexGrid's ComboBox. But it does not honor the type converter & all combobox items are shown as enum value strings. To handle this, we need to set ComboBox.FormattingEnabled to true for the ComboBox in the SetupEditor event & we also need to toggle ComboBox.DrawMode again to Normal since, by design, C1Flexgrid is set to use ComboBox.DrawMode as OwnerDrawVariable. Furthermore, the ComboBox.SelectedItem is not set according to the cell data. This behavior is also by design, since the properties change in the ComboBox when editing starts and they are not restored. There are two ways to make this work : Method 1 : Apply cell data to the selected item in the SetUpEditor event of the Grid.


//customize the combo to show converted values  
 private void c1FlexGrid1_SetupEditor(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)  
{  
    if (e.Col == 2)  
   {  
      //Enum-Combo-Column  
      ComboBox combo = (ComboBox)this.c1FlexGrid1.Editor;  
      combo.DrawMode = DrawMode.Normal;  
      combo.FormattingEnabled = true;  
      //Apply cell data to selected item!  
      combo.SelectedItem = (MyGreatEnum)this.c1FlexGrid1[e.Row, e.Col];  
   }  
}  

Method 2 : Undo your modifications when the editor closes in the LeaveEdit event of the Grid


//customize the combo to show converted values  
private void c1FlexGrid1_SetupEditor(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)  
{  
    if (e.Col == 2)  
    {  
       //Enum-Combo-Column  
       ComboBox combo = (ComboBox)this.c1FlexGrid1.Editor;  
       combo.DrawMode = DrawMode.Normal;  
       combo.FormattingEnabled = true;  
    }  
}  
// undo changes when editor closes  
private void c1FlexGrid1_LeaveEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)  
{  
   var combo = this.c1FlexGrid1.Editor as ComboBox;  
   if (combo != null)  
   {  
      combo.FormattingEnabled = false;  
      combo.DrawMode = DrawMode.OwnerDrawVariable;  
   }  
}  

Please refer to the attached images for better understanding : You can download the attached samples for detailed implementation. Download Sample CS Download Sample VB

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus