C# WinForms DataGridViewComboxColumn“;输入key";
我有一个带有可编辑组合框列的datagridview,但每次我在当前组合框上按“Enter”键时,我正在编写的文本都会消失C# WinForms DataGridViewComboxColumn“;输入key";,c#,.net,winforms,datagridview,datagridviewcombobox,C#,.net,Winforms,Datagridview,Datagridviewcombobox,我有一个带有可编辑组合框列的datagridview,但每次我在当前组合框上按“Enter”键时,我正在编写的文本都会消失 private void dgView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (dgView1.CurrentCell.IsInEditMode) { if (dgView1.CurrentCell.GetType() == typ
private void dgView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dgView1.CurrentCell.IsInEditMode)
{
if (dgView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
{
if (!((DataGridViewComboBoxColumn)dgView1.Columns[e.ColumnIndex]).Items.Contains(e.FormattedValue))
{
((DataGridViewComboBoxColumn)dgView1.Columns[e.ColumnIndex]).Items.Add(e.FormattedValue);
}
}
}
}
private void dgView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
ComboBox cbo = (ComboBox)e.Control;
cbo.DropDownStyle = ComboBoxStyle.DropDown;
}
}
我还尝试添加事件处理程序:“按下键”、“按下键”和“向上键”,但同样的问题也发生了
当我按“回车”键时,如何保持当前文本?DataGridViewComboBoxColumn不接受
项
集合中未包含的任何值。因此,当用户键入新值时,当前单元格在编辑后不会存储该值。您必须找到另一种方法在编辑单元格后立即获取最后一个值。我们可以在编辑控件Showing
事件处理程序中获得实际的DataGridViewComboBox编辑控件
,该控件实际上是一个组合框
,我们可以处理文本更改
事件。提交新值(添加到项并显示在当前单元格中)要处理的最佳DataGridView
事件是CellEndEdit
事件。下面是您应该执行的代码,我已经对其进行了测试,看起来它的工作方式与您预期的一样:
//use some variable to store the last edited value
string editingValue;
//EditingControlShowing event handler
private void dataGridView1_EidtingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e) {
var combo = e.Control as ComboBox;
if(combo != null){
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += (s,ev) => {
editingValue = combo.Text;
};
}
}
//CellEndEdit event handler for your dataGridView1
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
var comboColumn = dataGridView1.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
if(comboColumn != null && editingValue != "" &&
!comboColumn.Items.Contains(editingValue)){
comboColumn.Items.Add(editingValue);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = editingValue;
}
}
请注意,单元格值应为string
,这就是为什么editingValue
声明为string
,否则,您可能需要将编辑组合框的文本
转换为正确类型的编辑值
数据网格视图组合框列
不接受项
集合中未包含的任何值。因此,当用户键入新值时,当前单元格在编辑后不会存储该值。您必须找到另一种方法在编辑单元格后立即获取最后一个值。我们可以在编辑控件Showing
事件处理程序中获得实际的DataGridViewComboBox编辑控件
,该控件实际上是一个组合框
,我们可以处理文本更改
事件。提交新值(添加到项并显示在当前单元格中)要处理的最佳DataGridView
事件是CellEndEdit
事件。下面是您应该执行的代码,我已经对其进行了测试,看起来它的工作方式与您预期的一样:
//use some variable to store the last edited value
string editingValue;
//EditingControlShowing event handler
private void dataGridView1_EidtingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e) {
var combo = e.Control as ComboBox;
if(combo != null){
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += (s,ev) => {
editingValue = combo.Text;
};
}
}
//CellEndEdit event handler for your dataGridView1
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
var comboColumn = dataGridView1.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
if(comboColumn != null && editingValue != "" &&
!comboColumn.Items.Contains(editingValue)){
comboColumn.Items.Add(editingValue);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = editingValue;
}
}
请注意,单元格值应为string
,这就是为什么editingValue
声明为string
,否则,您可能必须将编辑组合框的文本
转换为正确类型的editingValue是否可以共享您的xaml代码没有xaml代码,Im使用windows窗体扫描您共享您的xaml代码没有xaml代码,Im使用windows窗体