1

我有:

  1. MenuStrip具有将DataGridView值保存到数据库的File>Save

  2. DataGridView具有必须包含数字值的列(我已经通过使用_CellValidating事件解决了这个问题,e.Cancel = true当输入不是数字时设置)

问题是当我在单元格仍然脏(在编辑模式下)时单击文件>保存时,它的值被忽略并且不包含在数据库记录中。

4

2 回答 2

2

At the beginning of the File>Save handler, call myDataGridView.EndEdit() to get out of edit mode and be able to get the value of the cell.

于 2012-06-16T13:58:04.747 回答
1

Try this if you want to immediately commit the cell changes to the underlying DataSource-

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

MSDN:

A cell is marked as modified if its contents have changed but the change has not been saved.

This event typically occurs when a cell has been edited but the change has not been committed to the data cache, or when an edit operation is canceled.

于 2012-06-16T13:59:21.417 回答