我有:
MenuStrip
具有将DataGridView
值保存到数据库的File>SaveDataGridView
具有必须包含数字值的列(我已经通过使用_CellValidating
事件解决了这个问题,e.Cancel = true
当输入不是数字时设置)
问题是当我在单元格仍然脏(在编辑模式下)时单击文件>保存时,它的值被忽略并且不包含在数据库记录中。
我有:
MenuStrip
具有将DataGridView
值保存到数据库的File>Save
DataGridView
具有必须包含数字值的列(我已经通过使用_CellValidating
事件解决了这个问题,e.Cancel = true
当输入不是数字时设置)
问题是当我在单元格仍然脏(在编辑模式下)时单击文件>保存时,它的值被忽略并且不包含在数据库记录中。
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.
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);
}
}
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.