如何创建自己的DataGridView列

时间:2015-08-15 08:43:37

标签: c# .net winforms datagridview

我想做的是制作

  • 显示不同项目的DataGridViewDropdownColumn 每个单元格的下拉列表。
  • 一个DataGridViewHybridColumn 显示每个单元格的TextBox或ComboBox。

我读了this toturial并创建了我自己的DataGridViewBarColumn,但我不知道如何实现这样的东西。

即使DataGridView中的行数超过100K,性能也不重要,我稍后会处理优化。

我认为他们也是这样做的,所以我在一个问题中问他们

1 个答案:

答案 0 :(得分:2)

我能给你的是实现它的起点:

首先,准备你的绑定并创建你的DataGridViewComboBoxColumn。

private List<string> list = new List<string>();

public Form1()
{
        InitializeComponent();

        // Data source for you dropdown list
        list.Add("First Option");
        list.Add("Second Option");
        list.Add("Third Option");

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.MultiSelect = false;
        dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
        dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
        dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        dataGridView1.DataError += dataGridView1_DataError;

        // Formatting your ComboBox
        DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
        comboColumn.FlatStyle = FlatStyle.Flat;
        comboColumn.HeaderText = "Hybrid Data Grid Column";
        comboColumn.DataPropertyName = "Tag";
        //comboColumn.DataSource = list;        

        dataGridView1.Columns.Add(comboColumn);
        dataGridView1.DataSource = list;

}

第二,输入一个事件进行编辑:

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        var combo = ((DataGridView)sender).CurrentCell as DataGridViewComboBoxCell;
        if (combo == null)
        {
            return;
        }
        if (!combo.Items.Contains(e.FormattedValue))
        {
            // Add the text you type into you bound list and refresh binding
            list.Add(e.FormattedValue.ToString());
            DataGridViewComboBoxColumn col = ((DataGridView)sender).CurrentCell.OwningColumn as DataGridViewComboBoxColumn;
            col.Items.Clear();
            col.Items.AddRange(list);
        }

        // Display what you type
        ((DataGridView)sender).CurrentCell.Value = e.FormattedValue;
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox comboControl = e.Control as ComboBox;
        if (comboControl != null)
        {
            if (comboControl.DropDownStyle != ComboBoxStyle.DropDown)
            {
                comboControl.DropDownStyle = ComboBoxStyle.DropDown;
            }
            //Update Items if list has changed (assumes items can't be deleted)
            if (comboControl.Items.Count != list.Count)
            {
                DataGridViewComboBoxColumn col = ((DataGridView)sender).CurrentCell.OwningColumn as DataGridViewComboBoxColumn;
                col.Items.Clear();
                col.Items.AddRange(list.ToArray());
                //Note: current cell's items don't appear to be refreshed by the lines above
                comboControl.Items.Clear();
                comboControl.Items.AddRange(list.ToArray());
            }
        }
    }

示例输出:

enter image description here

注意:由于我们在绑定列表中添加文本输入,因此一旦您点击下拉选项,您就会看到其他输入。所以,我会留给你去修饰它。