Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi gays,

i create windows application and in this app. i create 1 form, in this form i use data-grid view.


in grid i check some validation like value must be integer when validate cell....if value is not integer then i want to perform "e.cancel"
but in cell validated event e.cancel not work....any alternate solution???


thx in adv.
Posted
Comments
[no name] 20-Mar-13 8:12am    
Pleas provide the code.
Member 9444634 20-Mar-13 8:23am    
which code u want ????
Member 9444634 20-Mar-13 8:24am    
if (Convert.ToString(dgvItemDetails["QTY", e.RowIndex].Value).Contains("-.") )
{
MessageBox.Show(this, "Please enter proper numeric value.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//e.Cancel = true;
return;
}
Member 9444634 20-Mar-13 8:26am    
this code is write in cell validated event
ZurdoDev 20-Mar-13 9:03am    
You need to post all relevant code.

1 solution

Now that we have just about enough information to get an idea of what the problem is ...
You have put this code in the CellValidated event which
Quote:
"Occurs after the cell has finished validating."
to quote the text that appears in the properties window when you highlight that event.
Try putting the validation in the CellValidating event where e.Cancel will be available to you

[Edit] Attempting to make it clearer ...
C#
private void dgvItemDetails_CellValidated(object sender, DataGridViewCellEventArgs e)
 {
     // It is pointless to try to "validate" after the cell has validated
     //if (Convert.ToString(dgvItemDetails["QTY", e.RowIndex].Value).Contains("-."))
     //{
     //    MessageBox.Show(this, "Please enter proper numeric value.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     //    //e.Cancel = true;
     //    return;
     //}
 }

 private void dgvItemDetails_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
 {
     // This is the event to use to validate
     if (Convert.ToString(dgvItemDetails["QTY", e.RowIndex].Value).Contains("-."))
     {
         //This line does not compile in VS2010 MessageBox.Show(this, "Please enter proper numeric value.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         MessageBox.Show(this, "Please enter proper numeric value.", "Error Caption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         e.Cancel = true;   // This is now available to you
         return;
     }
 }
 
Share this answer
 
v2
Comments
Member 9444634 21-Mar-13 1:49am    
i think its really helpful!!!
can plz elaborate.

how can i write code("Occurs after the cell has finished validating") in cell validated event???
CHill60 21-Mar-13 5:45am    
Just move the code that you currently say you've put in the CellValidED event into the CellValidATING event. I'll Improve the solution to show you what I mean
Member 9444634 21-Mar-13 6:49am    
ya in cell validating e.cancel available but some other reason i dnt use this event.
CHill60 21-Mar-13 6:55am    
Well leave the code for the "other reason" in CellValidated and just move validation code into the CellValidating
Member 9444634 21-Mar-13 7:01am    
ya u r right.....same thing done

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