簡體   English   中英

如何在離開時雙擊,編輯和保存Windows Forms DataGridView CurrentCell?

[英]How can I “double-click, edit and save while-leaving” a Windows Forms DataGridView CurrentCell?

我對WinForm的DataGridView控件CommitEdit()實話,我很難嘗試混合並匹配其事件和方法(例如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

因此,您可能會注意到,您可以提供的任何幫助將是非常有價值的,並且將受到肯定。 提前謝謝你們!

哇,原來我正在處理的“實體”(LINQ-to-SQL)沒有PK,因此LINQ無法更新它,所以這不是DGV的錯。 對於那個很抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM