将comboBox下拉列表中突出显示的项目设置为单元格退出或单元格离开时的当前值

时间:2012-07-09 14:21:40

标签: c# winforms datagridview combobox datagridcomboboxcolumn

private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (this.dataGridViewSales.CurrentCell.ColumnIndex == 1)
  {
    ComboBox c = e.Control as ComboBox;
    ((ComboBox)c).AutoCompleteSource = AutoCompleteSource.ListItems;
    ((ComboBox)c).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    ((ComboBox)c).DropDownStyle = ComboBoxStyle.DropDown;
  }
}

使用上面的代码,我遇到一个问题,在从自动完成组合框中进行选择之后,当我通过按Tab键退出组合框Cell时,我放松了选择。有时候,选择会被保留,有时选择被清除,它会随机发生。

1 个答案:

答案 0 :(得分:0)

处理CurrentCellDirtyStateChanged事件解决了这个问题,我希望它不会导致其他一些问题!

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }