我是 C# 的新手,我真的被卡住了。我会尽量简洁。
我在 DataGridView 中有一些 ComboBox(在同一列中),我希望用户能够在 ComboBox 中键入。我得到它的半工作...
*问题1: 当我输入时,即使我输入的内容已添加到下拉列表中的选择列表中,组合框也会变为空白,我必须重新选择刚刚输入的值。我怎样才能使我键入的值保留为选择而不是空白。
*问题2: 有没有办法让某些 ComboBox 在同一列中不可编辑?我的代码似乎使我的所有 ComboBox 用户都可编辑。我怎样才能使一些 ComboBox 异常?
在此先感谢您,如果您能提供帮助的话!
代码如下:
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?
关于c# - 2 在 c# 中启用可编辑 DataGridViewComboBox(非数据绑定(bind))的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9029094/