我有一个连接到字符串列表的DataGridViewComboBoxCell:
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
List<string> data = new List<string>() { "88","LinearCurve", "NonLinearCurve" };
cell.DataSource = data;
cell.ReadOnly = false;
cell.Value = data[0];
dataGridView1.Rows[0].Cells[0] = cell;但是,当我双击单元格时,可以在运行时编辑这些字段吗?
当"88“被选中时,我应该可以双击并将文本更改为"89”。字符串列表数据现在应该包含"89“、"LinearCurve”和"NonLinearCurve“。
这里的要点是,输入要么是一个没有选择的数字,要么是一个预定义的字符串。
发布于 2016-03-11 20:09:48
为了编辑DataGridViewComboBoxCell中的值,我们将处理以下事件:
this.dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
this.dataGridView1.CellValidating += DataGridView1_CellValidating;
this.dataGridView1.DataError += DataGridView1_DataError;此外,为了将值更改永久化为cell.Value,我们将将您的设置更改为:
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.Items.AddRange("88", "LinearCurve", "NonLinearCurve");
cell.Value = cell.Items[0];
cell.ReadOnly = false;
dataGridView1.Rows[0].Cells[0] = cell;在处理EditingControlShowing时,我们的目的是在选择第一项(可编辑号)时将ComboBox.DropDownStyle更改为DropDown (可编辑),否则将更改为DropDownList (不可编辑):
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dataGridView1.CurrentCell == this.dataGridView1[0,0])
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= Cb_SelectedIndexChanged;
// Following line needed for initial setup.
cb.DropDownStyle = cb.SelectedIndex == 0 ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
cb.SelectedIndexChanged += Cb_SelectedIndexChanged;
}
}
}
private void Cb_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.DropDownStyle = cb.SelectedIndex == 0 ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
}处理CellValidating我们的目的是将第一项的值更改为用户输入的值,如果该值不是空的,并且不在列表中。我们还需要提交编辑更改并设置cell.Value
private void DataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex == 0)
{
var cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
if (cell != null && e.FormattedValue.ToString() != string.Empty && !cell.Items.Contains(e.FormattedValue))
{
cell.Items[0] = e.FormattedValue;
if (this.dataGridView1.IsCurrentCellDirty)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
cell.Value = e.FormattedValue;
}
}
}最后,处理DataError我们的目的是捕捉在ComboBox.Items中设置新值时抛出的错误。单元格会抱怨试图设置新值,但它仍然有效-因此可以忽略该错误:
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex == 0)
{
e.Cancel = true;
}
}附带说明:
如果您选择了一个不可编辑的值并开始在单元格上键入,它将尝试选择一个匹配的值。例如,如果列表中的第一项是"88“,并且您已经选择了"LinearCurve”,那么输入"8“将自动选择"88”--对于第一项,这将强制可编辑模式。所以看起来好像你已经开始编辑一个不可编辑的值了,但是你没有。这只是ComboBox的一个怪癖。
发布于 2016-03-11 11:35:44
如果将单元格的ReadOnly属性设置为false,它将使您编辑该单元格的内容。
试试这个:
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.ReadOnly = false;发布于 2016-03-11 11:38:43
设置组合框的值成员和显示成员
https://stackoverflow.com/questions/35938790
复制相似问题