开发者

How to validate form data entry before navigation with WinForms in .NET using DataBindingNavigator?

I'm just starting on .NET development with C#. Although I'm getting comfortable with the language itself I need some help with the peculiarities of the framework.

I'm building a windows forms application to replace a simple Access database. I have the data source connected, dropped controls on the form, put the basic logic in place, no problems there. Even figured out that I need to manually save the data back to the database using tableAdapterManager.UpdateAll(...) or similar.

My current problem, which I've been unable to find a resolution to after much googling, is how to validate my data entry before allowing the user to leave the current record.

Scenario:

User clicks 'Add' to get a new record, fills in some of the data but doesn't fill in one of the required fields. User then navigates to a different record without hitting the 'Save' button. Application throws an exception and the record is lost.

What e开发者_如何学编程vent can I intercept, or what function do I need to override to do pre-navigation validation of my form's data and cancel navigation if the data is invalid in some way?


Any Windows Form can handle FormClosing event where in you can put your validation code. The FormClosing Event has FormClosingEventArgs object, so if any of your validation fails, you can set its Cancel property to True to prevent navigating away.

EDIT

For a form that uses BindingNavigator object, you should be able to handle its "Validating" Event to achieve similar effect. In your BindingNavigator's Validating EventHandler, just set the Cancel Argument for its CancelEventArgs object, in case any of your validation(s) fail(s).


If you are using a datagridview object to present the values, you will need to handle the RowValidating Event

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜