我对WinForm的DataGridView控件几乎不陌生,说实话,我很难尝试混合并匹配其事件和方法(例如CommitEdit()
方法没有达到我的预期)实现“通过双击单元格进入编辑模式,修改其值(希望进行某种验证)并在离开上述单元格时保存更改的简单概念。我的实际代码看起来像这样,它肯定是不完整的:
// 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
因此,您可能会注意到您提供的任何帮助都将非常有价值并且非常受欢迎。非常感谢你们!
答案 0 :(得分:0)