我几乎没有使用WinForm的DataGridView控件,并且老实说我很难尝试混合和匹配它的事件和方法(例如,CommitEdit()
方法没有达到我所期望的)来实现简单的概念通过“进入编辑模式,双击单元格,修改它的值(希望做某种验证),并留下上述小区时保存更改我的实际代码看起来是这样的,它绝对是不完整的。如何“双击,编辑并保存” - Windows窗体DataGridView CurrentCell?
// DEBUG
private void myDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.myDataGridView.BeginEdit(true);
this.myDataGridView.CurrentCell.ReadOnly = false;
}
private void myDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
{
this.myDataGridView.EndEdit();
}
private void myDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewCellStyle editCellStyle = new DataGridViewCellStyle();
editCellStyle.BackColor = System.Drawing.Color.DarkOrange;
editCellStyle.ForeColor = System.Drawing.Color.Black;
this.myDataGridView.CurrentCell.Style.ApplyStyle(editCellStyle);
}
private void myDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCellStyle defaultCellStyle = new DataGridViewCellStyle();
defaultCellStyle.BackColor = System.Drawing.Color.White;
this.myDataGridView.CurrentCell.Style.ApplyStyle(defaultCellStyle);
}
// DEBUG
所以因为你可能会注意到任何你可以提供的帮助将是非常有价值的和definintely赞赏。非常感谢你们提前!
究竟是什么问题?例如数据源是否未更新?数据库? CellStyles没有更新? – 2010-07-13 15:33:49
感谢您的回复Conrad Frix。实际上没有任何更新,尽管
myDataGridView.CurrentCell.EditedFormattedValue
拥有新的价值,当它离开单元格时它已经丢失。 – 2010-07-13 15:41:16