简体   繁体   中英

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

I'm barely new with WinForm's DataGridView control and quite honestly I'm having a hard time trying to mix and match its events and methods (eg CommitEdit() method didn't do what I've expected) to implement the simple concept of "entering edit mode by double-clicking the cell, modify its value (hopefully doing some sort of validation) and saving changes when leaving the aforementioned cell. My actual code looks like this and it's definitely incomplete:

// 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

So as you might notice any help you could provide will be really valuable and definintely appreciated. Thanks much you guys in advance!

Wow, turns out the "Entity" (LINQ-to-SQL) I'm dealing with has no PK hence LINQ is unable to update it so it's not DGV's fault. Sorry about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM