0

在 DataGridView ComboBox 中选择一个值并单击 bindingnavigator 中的保存按钮后,数据库中的数据不会更新。用户必须失去焦点才能更新数据。有没有办法解决这个问题?

4

2 回答 2

1

诡异的。我最近做了这个,它就像一个魅力。在我的应用程序中,当 gridview 处于编辑模式(只读 false)时,当您选择单元格时,它将成为组合框,当您离开单元格时,它将表现为文本框。这是我所做的

void dgUpdateItems_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
        {
            dg[e.ColumnIndex, e.RowIndex].ReadOnly = true;
            return;
        }
        DataGridViewComboBoxCell cmbCell = new DataGridViewComboBoxCell();
        ComboUpdate(cmbCell);
        cmbCell.Value = ((DataGridView)sender)[e.ColumnIndex, e.RowIndex].Value.ToString();
        ((DataGridView)sender)[e.ColumnIndex, e.RowIndex] = cmbCell;
    }
}

void dgUpdateItems_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
            return;

        string str = dg[e.ColumnIndex, e.RowIndex].Value.ToString();
        DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)dg[e.ColumnIndex, e.RowIndex];
        string val = cmb.Value.ToString();

        dg[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
        dg[e.ColumnIndex, e.RowIndex].Value = val;

如果不理解,这是我代码的一部分,请告诉我。这是一个链接检查一下。它可能会有所帮助。 编辑模式下 DatagridView 中的 ComboBox

抱歉,忘了告诉你最重要的事情,即使专注,它也有效。如果您正在寻找其他东西,请在我头上扔石头。希望能帮助到你。

于 2011-09-30T14:20:47.960 回答
0

UpdateSource在保存之前尝试调用:

ComboBox c = Keyboard.FocusedElement as ComboBox;
if ((c != null) && (c.GetBindingExpression(ComboBox.TextProperty) != null))
  c.GetBindingExpression(ComboBox.TextProperty).UpdateSource();

高温高压

于 2011-09-28T08:08:31.750 回答