代码之家  ›  专栏  ›  技术社区  ›  Michael Neale

如何“双击,编辑和保存,同时离开”一个Windows窗体DataGridView CurrentCell?

  •  0
  • Michael Neale  · 技术社区  · 5 年前

    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
    

    所以,你可能会注意到,你所能提供的任何帮助都将是非常有价值的,并且非常感激。提前谢谢你们!

    1 回复  |  直到 14 年前
        1
  •  0
  •   Nano Taboada    14 年前

    哇,原来我处理的“实体”(linqtosql)没有PK,因此LINQ无法更新它,所以这不是DGV的错。很抱歉。