Dynamically add item to DataGridView ComboBox Column by typing in the cell

两盒软妹~` 提交于 2019-12-02 04:34:31

To dynamically add item to DataGridViewComboBoxColumn:

  1. Hanlde EditingControlShowing and get the DataGridViewComboBoxEditingControl
  2. Set editing control DropDownStyle to DropDown
  3. Handle Validating event of editing control and make sure you attach the event handler just once.
  4. Check if the Text of the editing control doesn't exists in the items:
    • Add it to data source of the column
    • Then reset data source of the column by setting it to null and assigning data source again.

Notes:

  • If you have multiple combo box, make sure you use different data sources for combo boxes and update corresponding data source in validating event.

  • If you handle the events using anonymous method, make sure you have a correct assumption about captured variables. To make it simple, you can handle the event using a normal method.

Example

The following example shows a DataGridView having two DataGridViewComboBoxColumn which for the second one, you can add new values by typing in the combo box at run-time.

To run the example, create a Form and drop a DataGridView on a new Form and just copy and paste the following code in the form:

private List<String> comboSource1;
private List<String> comboSource2;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    comboSource1 = new List<string> { "A", "B" };
    comboSource2 = new List<string> { "1", "2" };

    var dt = new DataTable();
    dt.Columns.Add("C1");
    dt.Columns.Add("C2");
    dt.Rows.Add("A", "1");
    dt.Rows.Add("B", "2");

    var c1 = new DataGridViewComboBoxColumn();
    c1.Name = "C1";
    c1.DataPropertyName = "C1";
    c1.DataSource = comboSource1;

    var c2 = new DataGridViewComboBoxColumn();
    c2.Name = "C2";
    c2.DataPropertyName = "C2";
    c2.DataSource = comboSource2;

    dataGridView1.Columns.AddRange(c1, c2);

    this.dataGridView1.DataSource = dt;
    dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
    dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    var dataGridView = sender as DataGridView;
    if (dataGridView?.CurrentCell?.ColumnIndex != 1) return;
    var comboBox = e.Control as DataGridViewComboBoxEditingControl;

    if (comboBox == null) return;
    comboBox.DropDownStyle = ComboBoxStyle.DropDown;
    if (!true.Equals(comboBox.Tag))
    {
        comboBox.Tag = true;
        comboBox.Validating += (obj, args) =>
        {
            var column = (DataGridViewComboBoxColumn)dataGridView.CurrentCell.OwningColumn;
            var list = comboBox.DataSource as List<string>;
            if (list == null) return;
            var txt = comboBox.Text;
            if (!list.Contains(txt))
            {
                list.Add(txt);
                column.DataSource = null;
                column.DataSource = list;
            }
            dataGridView.CurrentCell.Value = txt;
            dataGridView.NotifyCurrentCellDirty(true);
        };
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!