ns12345
ns12345

Reputation: 3105

Catching focus out event of a datagrid

I want to catch the focus out event of a datagrid so that everytime, datagrid in edit mode focuses out, I should be able to close out the edit mode.

Upvotes: 2

Views: 5739

Answers (3)

AlaaL
AlaaL

Reputation: 343

I think you don't want to let user edit cells values if that

use the dataGridView CellBeginEdit event

dataGridView1.CellBeginEdit += new System.Windows.Forms.DataGridViewCellCancelEventHandler(this.dataGridView1_CellBeginEdit);

and then cancel then edit in event handling

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        e.Cancel = true;
    }

i hope this help.

Upvotes: 1

Shekhar_Pro
Shekhar_Pro

Reputation: 18430

You can use the LostFocus event to do this.

//Short Version
gridview1.LostFocus += (sender, e) => {//Your code to close edit mode};

Or normally you would do it as:

//Normal long Version
gridview1.LostFocus += new EventHandler(gridview1_LostFocus);

some where define your method for event handeling

public void gridview1_LostFocus(object sender, RoutedEventArgs e)
{
     //Your code to close edit mode
}

Upvotes: 3

Related Questions