当选定特定的组合框项时,如何将DataGridViewComboBoxCell设置为可编辑模式

本文关键字:DataGridViewComboBoxCell 设置 模式 编辑 组合 | 更新日期: 2023-09-27 18:17:15

我有一个DataGridView控件,其第一列是DataGridViewComboBoxColumn。Combox元素的值例如:"Custom","AAA","BBB"。第二个DataGridView列是可编辑的单元格。当用户选择任何组合框项(除了"Custom")时,用户输入将移动到第二列单元格,以便他可以写入文本。

我想要实现的是,当用户选择"自定义",然后组合框值是可编辑的,所以用户可以输入自己的值。我试过使用"oncurrentcelldirtystatechangen"answers"editingcontrolshow",但这不起作用。通过"不工作",我的意思是它实际上将这个组合框设置为ComboBoxStyle。下拉,我可以编辑这个组合框项目文本后,我离开焦点从DataGridView行,然后用鼠标单击该组合框。但我需要它可以编辑后,"自定义"被选中。

    public void typeColumnDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
        {
            if (((ComboBox)e.Control).SelectedIndex == 0)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
            }
        }
    }
    public void typeColumnDataGridView_OnCurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dataGridView = sender as DataGridView;
        if (dataGridView == null || dataGridView.CurrentCell.ColumnIndex != 0) return;
        var dataGridViewComboBoxCell = dataGridView.CurrentCell as DataGridViewComboBoxCell;
        if (dataGridViewComboBoxCell != null)
        {
            if (dataGridViewComboBoxCell.FormattedValue != null)
            {
                if (dataGridViewComboBoxCell.FormattedValue.ToString() == "Custom")
                {
                    dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[0];
                    dataGridView.BeginEdit(true); //This does not work
                    return;
                }
                else if (dataGridViewComboBoxCell.FormattedValue.ToString() == "")
                {
                    return;
                }
            }
        }
        dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[1];
        dataGridView.BeginEdit(true);
    }

当选定特定的组合框项时,如何将DataGridViewComboBoxCell设置为可编辑模式

好了,看来我找到解决问题的办法了。

我的逻辑思维顺序如下:由于编辑DataGridView组合框单元格只有在失去焦点后才能工作,然后单击组合框,然后我需要以编程方式模拟此行为。

所以我修改了"OnCurrentCellDirtyStateChanged"事件如下:

        public void typeColumnDataGridView_OnCurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dataGridView = sender as DataGridView;
        if (dataGridView == null || dataGridView.CurrentCell.ColumnIndex != 0) return;
        var dataGridViewComboBoxCell = dataGridView.CurrentCell as DataGridViewComboBoxCell;
        if (dataGridViewComboBoxCell != null)
        {
            if (dataGridViewComboBoxCell.EditedFormattedValue.ToString() == "Custom")
            {
                //Here we move focus to second cell of current row
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[1];
                //Return focus to Combobox cell
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[0];
                //Initiate Edit mode
                dataGridView.BeginEdit(true);
                return;
            }
        }
        dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[1];
        dataGridView.BeginEdit(true);
    }

和" editingcontrolshow "事件:

        public void typeColumnDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control.GetType() != typeof (DataGridViewComboBoxEditingControl)) return;
        if (((ComboBox) e.Control).SelectedIndex == 0)
        {
            //If user selected first combobox value "Custom", make control editable
            ((ComboBox) e.Control).DropDownStyle = ComboBoxStyle.DropDown;
        }
        else
        {
            if (((ComboBox) e.Control).DropDownStyle != ComboBoxStyle.DropDown) return;
            //If different value and combobox was set to editable, disable editing
            ((ComboBox) e.Control).DropDownStyle = ComboBoxStyle.DropDownList;
        }
    }

还增加了"cellvalidation"事件,以自定义类型的值更新组合框值:

        public void typeColumnDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        DataGridView dataGridView = sender as DataGridView;
        if (dataGridView == null) return;
        if (!dataGridView.CurrentCell.IsInEditMode) return;
        if (dataGridView.CurrentCell.GetType() != typeof (DataGridViewComboBoxCell)) return;
        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (cell.Items.Contains(e.FormattedValue)) return;
        cell.Items.Add(e.FormattedValue);
        cell.Value = e.FormattedValue;
        if (((DataGridViewComboBoxColumn) dataGridView.Columns[0]).Items.Contains(e.FormattedValue)) return;
        ((DataGridViewComboBoxColumn)dataGridView.Columns[0]).Items.Add(e.FormattedValue);
    }
相关文章:
  • 没有找到相关文章