开发者

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

开发者 https://www.devze.com 2023-01-07 16:07 出处:网络
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 (e.g. CommitEdit() method didn\'t do what I\'ve expected)

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 (e.g. 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 advan开发者_StackOverflow社区ce!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消