Click here to Skip to main content
15,881,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!!

vs2008 + datagridview + cellvalidating event not fire....while change the value of particular cell.


any Help!!!
Posted
Updated 17-Oct-12 8:29am
v4
Comments
Pete O'Hanlon 17-Oct-12 6:05am    
This event only fires when the cell loses focus. Are you sure that focus has been lost?
Member 9444634 17-Oct-12 6:52am    
oh i think its fire at value change,

leave it, i want to check for decimal value enter or not. Key Press event is not fire dont know wts d problem.
Member 9444634 17-Oct-12 6:54am    
hello Arti you have any solution?

1 solution

If you want to validate data entry while still editing a cell you will need to set up a key press handler each time you begin editing a cell you need to validate.

To do that you must handle the DataGridview.EditingControlShowing event, and within that you will attach the keypress handler.

C#
private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    dataGridView1.EditingControl.KeyPress -= new KeyPressEventHandler(MyKeyPressHandler);
    dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(MyKeyPressHandler);
}


By checking the arguments to the EditingControlShowing handler you can be selective about the cells you decide to validate, or use different key press handlers for each cell.
Note that the key press handler is removed before it is added. This is to prevent a build up of identical handlers when you keep entering a cell. Removing a handler which doesn't exist (on first entry) isn't a problem.
 
Share this answer
 
Comments
Jothimani N 16-May-16 14:22pm    
i NEED CODE FOR VB.NET

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900