Windows Develop Bookmark and Share   
 index > Windows Forms General > How to select DataGridView original cell after DataValidated
 

How to select DataGridView original cell after DataValidated

If one has done what was needed in DataValidating (enter causes CellLeave), what is a good way to get the CurrentCell not to advance but remain in the edited cell?

mwindham  Tuesday, March 25, 2008 7:09 PM

Hi mwindham,

I guess the DataValidating you mentioned refers to the CellValidating event of the DataGridView control. If you want to prevent DataGridView’s focus from leaving the current cell in the CellValidating event, you can simply do this:

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

if (e.FormattedValue != 10)

{

e.Cancel = true;

}

}

Otherwise, there are three ways that cause the CurrentCell to change after the CellValidating event. They are: pressing the Enter key, pressing the Tab key and clicking the left button of the mouse when a cell is being edited.

You need to derive the DataGridView class and override the ProcessDialogKey, ProcessDataGridViewKey and WndProc methods to handle the above three situations respectively.

Please have a look at this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3077957&SiteID=1

Regards,

Jacob

Jacob Sui - MSFT  Monday, March 31, 2008 2:35 AM

Hi mwindham,

I guess the DataValidating you mentioned refers to the CellValidating event of the DataGridView control. If you want to prevent DataGridView’s focus from leaving the current cell in the CellValidating event, you can simply do this:

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

if (e.FormattedValue != 10)

{

e.Cancel = true;

}

}

Otherwise, there are three ways that cause the CurrentCell to change after the CellValidating event. They are: pressing the Enter key, pressing the Tab key and clicking the left button of the mouse when a cell is being edited.

You need to derive the DataGridView class and override the ProcessDialogKey, ProcessDataGridViewKey and WndProc methods to handle the above three situations respectively.

Please have a look at this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3077957&SiteID=1

Regards,

Jacob

Jacob Sui - MSFT  Monday, March 31, 2008 2:35 AM

Thanks for the directions. The ProcessDialogKey seems to let me do what I need, but I am unsure why the debugger traces through the ProcessCmdKey and seems to not have any affect as what follows is the ProcessDialogKey with the KeyData equal to Keys::Return and not Tab. There is another thread that is at times with some errors but interesting at

http://forums.microsoft.com/msdn/showpost.aspx?postid=157055&siteid=1&sb=0&d=1&at=7&ft=11&tf=0&pageid=2.

Code Snippet

public:

virtual bool ProcessCmdKey(Message% m, Keys KeyData) override

{

if (KeyData == Keys::Enter)

{

KeyData = Keys::Tab;

m.WParam = IntPtr((int) Keys::Tab);

}

return DataGridView::ProcessCmdKey(m, KeyData);

}

public:

virtual bool ProcessDialogKey(Keys KeyData) override

{

if (KeyData == Keys::Enter)

{

KeyData = Keys::Tab;

}

return DataGridView::ProcessDialogKey(KeyData);

}

mwindham  Monday, March 31, 2008 10:43 PM

You can use google to search for other answers

Custom Search

More Threads

• updating ListView from another Form
• Design time Support; use dropdown list
• RichTextBox - ZoomFactor problem
• need help with creating a custom datetime control
• Form opens to small, can't be resized. I've seen this question but can't find it. Now it's my turn.
• MouseDown event not fired when button held
• how to draw a rectangle on top of the main window?
• Hiding system menu on a form
• Validating in WinForms
• Problem with windows themes and child forms