C# 2在c中启用可编辑DataGridViewComboBox(非数据绑定)时出现问题#

C# 2在c中启用可编辑DataGridViewComboBox(非数据绑定)时出现问题#,c#,.net,winforms,datagridview,combobox,C#,.net,Winforms,Datagridview,Combobox,我对C#很陌生,我真的被卡住了。我会尽量简明扼要 我在DataGridView中有一些ComboBox(在同一列中),我希望用户能够在ComboBox中键入内容。我让它半工作 *问题1: 当我输入时,即使我输入的内容被添加到下拉列表中的选择列表中,组合框也会变为空白,我必须重新选择刚才输入的值。如何使我键入的值保持为所选内容而不变为空 *问题2: 有没有办法使某个组合框在同一列中不可编辑?我的代码似乎使我的所有组合框用户都可编辑。如何创建一些组合框例外 如果你能帮忙的话,请提前谢谢你 代码如下:

我对C#很陌生,我真的被卡住了。我会尽量简明扼要

我在DataGridView中有一些ComboBox(在同一列中),我希望用户能够在ComboBox中键入内容。我让它半工作

*问题1: 当我输入时,即使我输入的内容被添加到下拉列表中的选择列表中,组合框也会变为空白,我必须重新选择刚才输入的值。如何使我键入的值保持为所选内容而不变为空

*问题2: 有没有办法使某个组合框在同一列中不可编辑?我的代码似乎使我的所有组合框用户都可编辑。如何创建一些组合框例外

如果你能帮忙的话,请提前谢谢你

代码如下:

private void dm_dgview_add_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                DataGridViewComboBoxEditingControl combo = e.Control as    DataGridViewComboBoxEditingControl;
                combo.DropDownStyle = ComboBoxStyle.DropDown;
                combo.TextChanged += new EventHandler(combo_TextChanged);
            }
        }

        void combo_TextChanged(object sender, EventArgs e)
        {
            dm_dgview_add.NotifyCurrentCellDirty(true);

        }

        private void dm_dgview_add_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridViewComboBoxCell cell = dm_dgview_add.CurrentCell as DataGridViewComboBoxCell;

            if (cell != null && e.ColumnIndex == dm_dgview_add.Columns[1].Index)
            {
                if (!cell.Items.Contains(e.FormattedValue) && e.FormattedValue != null)
                {
                    cell.Items.Add(e.FormattedValue);
                }
            }
        }

请帮忙,我非常感激

是否需要在单元格验证事件中设置cell.Items.SelectedItem=e.FormattedValue?

是否需要在单元格验证事件中设置cell.Items.SelectedItem=e.FormattedValue