Home All Groups Group Topic Archive Search About

VB2005 - Stop User from Leaving Row in DataGridView

Author
31 Dec 2005 1:10 AM
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

Author
31 Dec 2005 8:23 AM
Cor Ligthert [MVP]
Matt,

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

http://www.vb-tips.com/default.aspx?ID=bece831d-6742-4364-bd0d-203ca99d2825

I hope this helps,

Cor

Show quoteHide quote
> 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
Author
31 Dec 2005 6:42 PM
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

Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Matt,
>
> I assume this is the same with a DataGridView (not tried yet)
>
> http://www.vb-tips.com/default.aspx?ID=bece831d-6742-4364-bd0d-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
>
>
Author
3 Jan 2006 6:36 AM
Jeffrey Tan[MSFT]
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.
Author
10 Jan 2006 12:45 AM
Matt
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:
Show quoteHide quote
> 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.
Author
10 Jan 2006 2:02 AM
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

Matt wrote:
Show quoteHide quote
> 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.
Author
17 Jan 2006 7:46 AM
Jeffrey Tan[MSFT]
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.
Author
17 Jan 2006 4:58 PM
jpenn44
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