VB2005 - Stop User from Leaving Row in DataGridView

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

In many places in my application, I have a DataGridView which users can
either enter data into or use as a method of navigation in a
master/detail form view. I need a method of stopping the user from
leaving a row if there is invalid data in it (e.g., a field that cannot
be null is). Because there is no Cancel argument on the parameters for
the RowLeave event, I can manage to catch the error and throw up a
message, but I am unable to stop it from happening in the first place.
Has anyone found a way around this problem?

Thanks,
Matt
 
Cor,
Unfortunately, validating isn't my issue. I can check to see that there
is an issue, and throw the appropriate error message if there is. The
problem is being unable to cancel the moving from row to row (and, as
such, attempting to save the changes). As far as I can tell, the code on
your site only helps with validating the data, not stopping the RowLeave
event entirely.

Regards,
Matt
 
Hi Matt,

Thanks for your post.

For DataGridView, it has a CellValidating and RowValidating events, these 2
event handlers take
DataGridViewCellValidatingEventArgs/DataGridViewCellCancelEventArgs type
paramters. Both these 2 types have a property named Cancel. We can set
Cancel property to true to prohibit the user from selecting another row.
This should stop RowLeave event entirely.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thank you Jeffrey. I did not realize that Canceling the validating event
would cancel all of the following events as well.
 
Hi Matt,

I am glad you got what you want. If you need further help, please feel free
to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey,
Though I original thought that this would work, I have since realized
that the RowValidation event does not trigger at a time which makes it
feasible to stop a user from leaving a row when there is theoretically
invalid data in the record.

The issue is that the validation event seems to pull information from
the row you're entering, as opposed to the row you're leaving. For
instance: I want to check if the FirstName field is empty ("") and stop
the user from moving if it is. If I try to create a new record, it
triggers the validation event, sees that the FirstName field on the new
record is empty, and throws up my message box. Is there any way to make
solely use the information on the row it was leaving?

Thanks,
Matt
 
Update: It appears it simply validates both when you are attempting to
exit a row and when you are attempting to enter one. So the real
question would be how to stop it from running the function if it's
entering the row.

Thanks,
Matt
 
Hi Matt,

Thanks for your feedback.

I am not sure I understand your problem very well. If I use the following
code to dump out the cell content in RowValidating event, I will always get
the original leaving row content, not the entering row content:
private void dataGridView1_RowValidating(object sender,
DataGridViewCellCancelEventArgs e)
{

Console.WriteLine(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].V
alue.ToString());
}
Also, we should write the validating logic code. So we should check the
leaving row and validate it, if not valid, we can set e.Cancel to true to
disable the leaving operation.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top