By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

VB2005 - Stop User from Leaving Row in DataGridView

P: n/a
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
Dec 31 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Matt,

I assume this is the same with a DataGridView (not tried yet)

http://www.vb-tips.com/default.aspx?...d-203ca99d2825

I hope this helps,

Cor
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

Dec 31 '05 #2

P: n/a
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

Cor Ligthert [MVP] wrote:
Matt,

I assume this is the same with a DataGridView (not tried yet)

http://www.vb-tips.com/default.aspx?...d-203ca99d2825

I hope this helps,

Cor
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


Dec 31 '05 #3

P: n/a
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.

Jan 3 '06 #4

P: n/a
Thank you Jeffrey. I did not realize that Canceling the validating event
would cancel all of the following events as well.

Jeffrey Tan[MSFT] wrote:
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.

Jan 3 '06 #5

P: n/a
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.

Jan 4 '06 #6

P: n/a
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

Jeffrey Tan[MSFT] wrote:
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.

Jan 10 '06 #7

P: n/a
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

Matt wrote:
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

Jeffrey Tan[MSFT] wrote:
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.

Jan 10 '06 #8

P: n/a
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.
Jan 17 '06 #9

P: n/a

Matt,

I was having the same issue, and have found a work around (maybe not
ideal, but it works).

Since you are already validating your fields, and throwing up the error
message, set a switch for valid row.

Then, what you need is an event AFTER, all the row leaves, row enter,
cell enter events. (I use the event that updates my time on the status
bar).

Check the switch for validity, if Yes, simply do nothing, if it is not
valid, set the current cell back to the row you just left. (This
assumes capturing the row number and saving it. I also capture the
column number of the bad field so I set the focus to the invalid
field).

Again, I am sure this isn't the "perfect" solution, but after days of
pounding my head against the wall, this is what I came up with.
--
jpenn44
------------------------------------------------------------------------
jpenn44's Profile: http://www.hightechtalks.com/m744
View this thread: http://www.hightechtalks.com/t2319951

Jan 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.