我有一个DataGridView控件,其第一列是DataGridViewComboBoxColumn。 Combox元素的值可以说是:“Custom”,“AAA”,“BBB”。第二个DataGridView列只是可编辑的单元格。当用户选择任何组合框项目(“自定义”除外)时,用户输入被移动到第二列单元格,以便他可以写文本。
我想要实现的是当用户选择“自定义”时,ComboBox值是可编辑的,因此用户可以输入自己的值。 我尝试过使用“OnCurrentCellDirtyStateChanged”和“EditingControlShowing”,但这不起作用。 通过“不工作”我的意思是它实际上将这个组合框设置为ComboBoxStyle.DropDown,我只能在从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);
}
答案 0 :(得分:1)
好的,似乎我找到了解决问题的方法。
我的逻辑思维序列如下: 由于DataGridView组合框的编辑仅在失去焦点然后再点击组合框后才能工作,所以我需要以编程方式模拟这种行为。
所以我修改了#34; 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);
}
还有" EditingControlShowing"事件:
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;
}
}
还添加了" CellValidating"使用自定义类型值更新Combobox值的事件:
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);
}