Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView remain in cell after Enter pressed
 

DataGridView remain in cell after Enter pressed

Hi, how can I make the DataGridView remainina cell after Enter is pressed to commit an edit? The default behaviour is to move down one cell. The only way that I could make it work was to inherit the DataGridView and overrideProcessCmdKey.When the Enteris pressed,I would move one cell to the left and then back. See code below. This is a hack and I would like a better way of handling it. Also, I am using the DataGridView unbound.
 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (msg.WParam == new IntPtr(13)) { this.CurrentCell = this[this.CurrentCell.ColumnIndex-1, this.CurrentCell.RowIndex]; this.CurrentCell = this[this.CurrentCell.ColumnIndex, this.CurrentCell.RowIndex]; return true; } return base.ProcessCmdKey(ref msg, keyData); }
johnsheares  Tuesday, September 08, 2009 1:35 AM
Hello johnsheares,

I think override the ProcessDialogKey can meet your need. Here is the example
public class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey (Keys keyData)
{
if (keyData == Keys.Enter)
{
this.EndEdit();
return true;
}
return base.ProcessDialogKey(keyData);
}
}
ProcessDialogKey is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESCAPE, and arrow keys. So you can handle it to call EndEdit method.

Please tell me if you have any problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, September 09, 2009 3:56 AM
Hi,
i hope it will help you..
 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {           
                e.Cancel = true;
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Tuesday, September 08, 2009 3:43 AM
Nope, that doesn't help at all.
johnsheares  Tuesday, September 08, 2009 4:02 PM
Hello johnsheares,

I think override the ProcessDialogKey can meet your need. Here is the example
public class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey (Keys keyData)
{
if (keyData == Keys.Enter)
{
this.EndEdit();
return true;
}
return base.ProcessDialogKey(keyData);
}
}
ProcessDialogKey is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESCAPE, and arrow keys. So you can handle it to call EndEdit method.

Please tell me if you have any problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, September 09, 2009 3:56 AM

You can use google to search for other answers

Custom Search

More Threads

• datagridview combobox problem
• Refreshing a combobox from another form
• Data Bound Applications with ADO.NET and Custom Objects
• Bind data source to DataGridView RowHeader value
• fileds add
• crystal Report command problem
• row header in datagridview
• Specific problem: i want to highlight some specific row using databinding
• Datagrid - Capturing Updates To Cells
• DataGridView Master/Slave bug???