How to discard row if gridview's row validation fails (devexpress) - c#

In XtraGridView, from the way RowValidation works, it seems that user has no choice but first correct the values (leading to validation being successful) and then press Discard button (which I provided separately and it does RejectChanges). I want an option that user can discard the row without any pop-up alert even if row has invalid data.
In my case when user presses Discard, it leads to row validation being fired (due to focus change). The discard button event handler is suppressed, if row validation is unsuccessful. This way I am never able to simply discard the invalid row.
This is common feature anyone with a grid would need. Expecting a standard solution or a workaround.

You made me curious, as you say,something as basic as that should be in there somewhere.
Bit of gooling found that waht you do is trap the InvalidRowException, it has a var argument and you can set it to ignore.
Here's where I found it.
DevExpress

Can't you check the focused button and sidestep validation if the discard button is focused.
The general DevExpress setup forces the user to insert valid values when validating, pressing Esc is a simple solution to reset the field to the original value.

Related

Validation DataGridView Windows Forms

I have two forms with grid views, one made from a database data source and one from entity framework dbset's. The EF one, when I press add row it adds a row and I can edit the ID. The other form, when I add row the ID is negative. Rather than incremented from the previous. Not sure why. But the issue I'm having is that when I click out of a column I added without filling it all in it validates and gives an error. I want this to happen at the end when save is clicked.
Right now I have these event handlers for the tables:
AddNewItem_Click, SaveItem_Click, DeleteItem_Click, CellContentClick, and more importantly: CellValidating, CellValidated, CellEndEdit, and most importantly CellValueChanged.
I read somewhere that using CellValueChanged is the best way to assure that the validation does not occur when I change cells. But I can't find an example. Basically, as far as this website validated says about validating and validated, there are steps that occur when you change cell with mouse, I don't care about keyboard.
Enter 2. GotFocus 3. LostFocus 4. Leave 5. Validating 6. Validated
When the cell loses focus, I get the error because it failed the validating. However on my EF created form, this does not occur. How can I stop this behavior from validating every time a cell loses focus?
My other question is this: as I said before in my non-EF form with the DataGridViews, when I add a row it adds a negative number as the ID which is the PK. I also can't edit that cell by clicking in it. In the EF form, I can click in the cell and edit it, and it doesn't add a negative number and while it may not make the numbers consecutive I can edit them myself and fix them. Why am I having these difference between the two? The only difference is one's datagridviews are created from EF data source and the others are created from a raw sql database data source.
So how can I change cell without validating, and how can I get these ID's to not be negative? Also how can I make the cells in ID column editable like in the EF form.
Any advice would help a lot, and examples help me a ton as I'm a visual learner.
If you need any code or information please ask and I'll post it, I'm not sure what to post as there hasn't been customization in the event handlers.
Thanks
The answer is to add this.CausesValidation = false to the event handlers that are being triggered.

DataGridView: Bailing out of cell validation

I'm handling CellValidating and setting e.Cancel = true if the data in the cell is invalid. This almost gets me where I want to be, but the problem is if the user has some invalid data in the cell, the rest of the UI is essentially off-limits until they fix the error or press Esc.
Since pressing Esc might not be intuitive to certain users, they may find it frustrating that they can't e.g. click on the "Back" button I have on the form to leave the screen altogether. (In such a scenario, their change-in-progress should be discarded as if they had pressed Esc.)
Any ideas on how to achieve this? I do like that they aren't allowed to start editing other cells without fixing errors in their current cell, but I'd prefer they still be able to press the "Back"/"Cancel" buttons on the form.
Thanks in advance!
You could set the CausesValidation property to false.

C# / Filter input of a textbox and display notification balloon

I'm wondering how to filter the input of a .NET textbox.
I already know that I could listen for the KeyDown event and intercept the key, but that won't filter pasted strings by a right-click menu or a CTRL+V.
I also don't wan't to completely disable the possibility of pasting of characters in the textbox. The paste action should be cancelled whenever it contains one or more invalid characters.
Finally, I'd like to display a notification balloon whenever invalid characters are either entered or pasted.
μTorrent already has this exact behavior:
How can I achieve this functionality in C# ?
TextChanged event - Seems like a good call.
You can spawn your own baloon or ToolTip on any control you want to show a detailed feedback to the user
It seems like a combination of KeyPress, TextChanged, Validating, and Validated events should work for your purposes.

C# UserControl Validation

I have a UserControl with a Tab Control containing three tabs. Within the tabs are multiple controls - Datetimepickers, textboxes, comboboxes.
There is also a Save button which when clicked, calls this.ValidateChildren(ValidationConstraints.Enabled) Now, I click save and a geniune validation error occurs. I correct the error and then click save again - valdiation errors occur on comboboxes on a different tab. If I navigate to this tab and click save, everything works fine. How can this be? I haven't changed any values in the comboboxes so how can the fail validation then pass validation?
The comboboxes are bound to a dataset with their selectedValue and Text set. I just don't understand what is happening here.
This behaviour also occurs for some textboxes too. The validation rule is that they have to be a decimal - the default value is zero, which is allowed. The same thing happens, they fail validation the first time - I make no changes, click save again and they pass validation.
EDIT:
The error is not a Framework error - the error is the one I provide during my validation routine. e.g Select a valid drop down option.
I stepped through the process and the sometimes the combobox values are the default values other times the selectedvalue is the default but the text is null. I don't see how this can happen if I'm not changing anything in the combobox.
If you need any further information please let me know
thanks
Barry
The following is a quote from MSDN:
"Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."
So I'm guessing that if before you press save the first time, you make sure that you've clicked on each tab at least once, it'll work as expected, but if you don't look at all tabs before saving, it fails?
Have you checked that the default value is not null ?

Override tab behavior in WinForms

I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.

Categories