473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VB2005 - Stop User from Leaving Row in DataGridView

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
9 15368
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
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mark | last post by:
I am designing a game for a forum. When the user has finished playing I need to save their data to a cookie then navigate to a page which holds their score data (I can't have both sets of data on...
8
by: Zvonko | last post by:
Hi! Is it possible to capture an event when user leaves the page and execute some code? Not when he closes the window. Any ideas?
0
by: alex1985 | last post by:
I would like that the user can change the datagridview to his own wishes and that when he closes the application and start the application again that his changes are still there. I thought that would...
18
by: sebastian | last post by:
Can this be done without client-side scripting? I thought sessions might be the answer but after some testing found that sometimes the old session data remained even after the user left the site...
5
by: =?Utf-8?B?QW5keQ==?= | last post by:
As per the question really. Not trying to stop them leaving, but just detect WHEN they leave the site
4
by: daveh551 | last post by:
Okay, I feel like this is probably a stupid question, but... I have accessed sites that are pushing some scam that, when you go to leave the page it will pop up a box saying, "No, don't go, let...
6
by: michelle eccles | last post by:
I have an array with limit of 5 values. The code as writen allows the user to enter a 6th number before averaging the first five number. I want to stop the user from being able to enter that 6th...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.