DataGridView autoComplete comboBox列不保留初始单元格的值

时间:2012-07-17 08:09:24

标签: c# winforms datagridview datagridviewcolumn datagridviewcomboboxcell

我有一个带有autoComplete组合框列的绑定dataGridView,并且自动完成工作正在工作,除了我正在观察一个令人恼火的行为。

当我第一次在自动完成单元格中键入文本并使用tabKey移动到下一个单元格时,我的选择不会保留,我选择的内容将被清除,自动完成单元格将保留为空。如果ii立即使用左箭头键返回到该自动完成单元格并键入文本,我保留的内容将保留,不会出现任何问题。

所以,我遇到的问题是让单元格保留我的第一个初始选择,现在唯一的工作是Tab到下一个单元格,然后返回到这个有问题的autoComplete组合框单元格并全部打字。在这一点上,它的工作原理。

我应该处理一些事件,以便在单元格假期上提交我选择的文本吗?

代码:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
        }
    }

编辑:在下面的CellLeave中,即使我做了选择,值也会返回为null。

 private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        var Row = this.dataGridView1.CurrentRow.Index;
        string value = this.dataGridView1["itemID", Row].Value.ToString();
    }

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

非常简单,这可以通过调用 notifycurrentcelldirty event on editingcontrol showing event.

来完成
Private Sub dataGridView1_EditingControlShowing(sender As Object, 
                         e As Forms.DataGridViewEditingControlShowingEventArgs) 
                         Handles Me.EditingControlShowing
            dataGridView1.NotifyCurrentCellDirty(True)
End Sub

Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles Me.CurrentCellDirtyStateChanged
            If IsCurrentCellDirty = True Then
                CommitEdit(Forms.DataGridViewDataErrorContexts.Commit)
            End If
End Sub