2010-07-13 76 views
0

我几乎没有使用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赞赏。非常感谢你们提前!

+0

究竟是什么问题?例如数据源是否未更新?数据库? CellStyles没有更新? – 2010-07-13 15:33:49

+0

感谢您的回复Conrad Frix。实际上没有任何更新,尽管myDataGridView.CurrentCell.EditedFormattedValue拥有新的价值,当它离开单元格时它已经丢失。 – 2010-07-13 15:41:16

回答

0

哇,结果我处理的“实体”(LINQ-to-SQL)没有PK,因此LINQ无法更新它,所以它不是DGV的错。对于那个很抱歉。

+0

我已经能够在实体模型中“伪造”PK,它可以工作,尽管我不一定热爱它的工作“解决方法”。 – 2010-07-21 13:58:37