概述

  1. FlexGrid for WinForm 采用了最新的数据绑定技术,并与Microsoft .NET Framework无缝集成。 因此,您可以获得易于使用的灵活网格控件,用于创建用户友好界面,以显示编辑格式化组织汇总和打印表格数据。

  2. FlexGrid的DataMap属性允许您实现“已翻译”的行或列。在转换的行或列中,网格不显示存储在单元格中的值。相反,它会在列的DataMap中查找这些值并显示映射的值。

  3. 有时您可能需要在C1FlexGrid / C1FlexGridClassic中使用DataMap来显示项目列表。即使列表包含大量数据,其加载也是平滑且即时的。在本文中,我们将讨论如何使用自定义ComboBox编辑器以加快DataMap网格的加载时间。

创建编辑器并在Grid中托管它

所有内置网格编辑器都实现IC1EmbeddedEditor接口,ComponentOne Input库中的控件也是如此。 如果我们想要使用带有C1FlexGrid的第三方编辑器,我们需要创建一个派生类并实现此接口。

实现步骤

创建一个模型类MyComboItem来绑定ComboBox。

public class MyComboItem

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

创建一个自定义控件MyComboBox,它继承ComboBox类并实现IC1EmbeddedEditor接口。

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
    }
}

创建MyComboBox类的实例,并将其分配给网格的列编辑器,如下所示:

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.