FlexGrid for WinForms incorporates the latest in data-binding technology and integrates seamlessly with the Microsoft .NET Framework. As a result, you get an easy-to-use, flexible grid control for creating user-friendly interfaces that display, edit, format, organize, summarize, and print tabular data.

FlexGrid’s DataMap property allows you to implement "translated" rows or columns. In translated rows or columns, the grid does not display the values stored in the cells. Instead, it looks up those values in the column's DataMap and displays the mapped value.

Sometimes you may need to use DataMap in C1FlexGrid/C1FlexGridClassic to display a list of items. Even if the list contains a large number of items, it is important that the loading is smooth and instant. In this article, we will discuss how to use a custom ComboBox Editor in order to speed up the loading time of a DataMap grid.

Creating the Editor and hosting it in Grid

All built-in grid editors implement the IC1EmbeddedEditor interface, as do the controls in the ComponentOne Input library. If we want to use a third-party editor with C1FlexGrid, we would need to create a derived class and implement this interface.

Implementation

Create a model class MyComboItem to bind the ComboBox.

public class MyComboItem
            {
                public int Id { get; set; }
                public string Display { get; set; }
        }

Create a custom control MyComboBox which inherits ComboBox class and implements IC1EmbeddedEditor interface.

public partial class MyComboBox : ComboBox, IC1EmbeddedEditor
    {
        public MyComboBox()
        {
            InitializeComponent();
        }
        #region IC1EmbeddedEditor-Members
        // Initialize editor: select transferred value
        public void C1EditorInitialize(object value, IDictionary editorAttributes)
        {
                this.SelectedValue = value;
        }
        //Get value from editor
        public object C1EditorGetValue()
        {
            return (base.SelectedItem as MyComboItem)?.Id; 
        }
        //Value is always TRUE
        public bool C1EditorValueIsValid()
        {
            return true;
        }
        //Adjust editor size
        public void C1EditorUpdateBounds(Rectangle rc)
        {
            if (rc.Height != -1 && rc.Width != -1)
            {
                this.Location = new Point(rc.X, rc.Y);
                this.Width = rc.Width;
                this.Height = this.DefaultSize.Height;
            }
            else
            {
    //Editor has scrolled out of the picture. Take over the height / width of -1.
                this.Width = -1;
                this.Height = -1;
            }
        }

        //TRUE if Escape or Enter
        public bool C1EditorKeyDownFinishEdit(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Enter)
                return true;
            return false;
        }

        //Format and editor value
        public string C1EditorFormat(object value, string mask)
        {
            return null;
        }

       //Style of Editors
        public UITypeEditorEditStyle C1EditorGetStyle()
        {
            return UITypeEditorEditStyle.DropDown;
        }
        #endregion
    }
}

Create an instance of MyComboBox class and assign this to the grid’s column editor as follows:

             Dictionary<int, string> DMap = new Dictionary<int, string>();
            ComboBox c1 = new MyComboBox();
            List<MyComboItem> _list = new List<MyComboItem>();              
            c1.DataSource = _list;
            c1.ValueMember = "Id";
            c1.DisplayMember = "Display";            
            _flex.Cols[2].Editor = c1;
           _flex.Cols[2].DataMap = DMap; //use DataMap to show IDs as values.

And, you are good to go.

Try it for yourself.

Download the sample

Speed up your DataMap loading with ComponentOne.

Download the latest version of ComponentOne Studio Enterprise

Download Now!