Validation DataGridView Windows Forms - c#

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.

Related

How to change ordering of 'CurrentCellChanged' and 'SelectedCellsChanged' effects?

When selecting a new cell on a new row in a WPF DataGrid, it seems that this is done through two sequential events that take path "A" in the figure below. These are the following events:
CurrentCellChanged (for shifting the selection within a current row)
SelectedCellsChanged (for shifting the selection of rows)
Due to some background actions that I have going on with the DataGrid and its bound DataTable, I need to be able to mimic something like taking path "B". I can't find any other events that are triggered when clicking the next cell that allow me to do this.
Does anyone have any idea what I could do here?
I have one idea that should work, but it seems like a hack and I imagine there must be a better way that works with the DataGrid and not around it.
Additional Info
I am doing a sort of independent row-by-row cell selection marker, so I use CurrentCellChanged to perform an action if a user changes a selection within a row to a cell that meets certain criteria. However, if the selection is on a different column of another row, the action is still first performed on the deselected row, and then again on the newly selected row once SelectedCellsChanged occurs.
If path "B" were taken, my setup would work correctly.
For now I'm using the following method to avoid this problem:
When the CurrentCellChanged event causes a change in markers on a row, a private boolean property of the form is set to record 'True' for a changed event, and some before & after values for one of the changes is also recorded.
If the SelectedCellsChanged event occurs, the routine checks for this boolean flag. If it indicates a change, the actions in CurrentCellChanged are reversed for the appropriate row using the recorded data before the action is then done in SelectedCellsChanged. Since as far as I can tell SelectedCellsChanged never occurs alone and always follows CurrentCellChanged, so this method should be reliable (famous last words).
Sorry for the lack of code, but it seems to me that the drawing and event names should suffice for describing the strategy that I need! If code samples would help, I can provide some if you indicate just what you need to see.

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

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.

Winform DataGrid with detail, what is the best way

I'm pretty new to c# and Winforms and I'm wondering what is the best approach to the following screen design.
I have a window that contain a Datagrid wich would be read-only. Beneath the grid, I have the detail of the records in differents fields (textbox, combobox, checkbox).
What I want is that when the user click on an item in the datagrid, the data will be shown in the detail fields.
That part is pretty easy, but I want to be able to update the fields automatically, wich means, I would prefer to not have to press a Save button.
Let's say that I click an item in the datagrid, change some value in the detail fields and the I click on another item in the datagrid, then I also want to perform some validation and calculation before the record get updated.
What I was thinking at first was to get the button for "new", "edit", "save" action and lock and unlock the fields accordingly and keep a flag to know if i need to insert or update the data, but then I realized that I would prefer to not have thoses button and have the save performed automatically.
Is there any sample somewhere that does what I want?
Also, would you guys using the built-in databinding functunality or just use a dataset object in code?
Pretty common scenario.
On selected row change of grid you know which datarow you shoul bind to the other controls. when same event happens again you validate and save or cancel in case of errors.
You can make use of DataGridView.CellEndEdit Event to get the new value and DataGridView.CellBeginEdit event to get the old value and update your data if there is any change

CellValueChanged vs CellValidating Events for DataGridView

What's the best place to implement validation logic code and conditional formatting code for a DataGridView?
In a lot of books and articles that I've read on this control, it seems to suggest that the appropriate event to handle for this is the CellValidating one.
Well, the name more than implies this as well.
However, this event triggers a bit too often for my tastes and I'm not sure it is required.
For example, this event triggers everytimes the users switches to another row.
On the other hand, the CellValueChanged event seems to trigger only when the value of the cell changes, which means the validation code runs only when the value changes and not everytime a user changes cells.
Now, since so many books use the CellValidating event, I wonder if there is not any gotcha (in display for example) with using the CellValueChanged?
I understand that the impact in performance should be irrelevant when using simple validation and conditional highlighting rules but I would as much prefer it not to run useless code everytime the user moves to another cell if it can be avoided.
Thanks,
I'm using CellValueChanged currently on a grid with custom validation and have had no problems with display or anything else.
I used this event because I wanted to fire off a certain order of events, but only when the user changes the value of a cell.
I have not noticed much in the way of a performance hit (tested with 100 - 5000 rows).
I think in the end it depends on what your validation needs are. In my case, CellValueChanged has done what I wanted/needed.
EDIT
The biggest thing about the CellValidating event is that you can stop the user from leaving a cell, if the value entered does not pass your validation. I didn't want to do this.
Its simple, inside the CellValidatingEvent you need to check if a condition is right. If your condition is false then you just add this code e.cancel. It will prevent the cursor to lost its focus

DataGridView - How does one hook into the event associated with a cell change?

I'm using a DataGridView with C#.NET. When a user is editing a column, I need another column in the same grid to change with every keystroke/change. How/where do I insert my own code for this type of event?
What you're trying to achieve is not possible without a bit of work. By default the DataGridView class does not provide a CellChanging style event. Instead it provides book ended events in the form of CellBeginEdit and CellEndEdit.
Part of the reason is likely due to the varying ways in which a cell can be editted. Having a CellChanging would make sense for a text style cell but wouldn't make as much sense for say a Button style cell.
You could easily implement a solution though which propagated the value once it was completely entered via the above said events.
The only way I can see to implement it for every keystroke would be to
Handle the above events
Figure out what the runtime type of DataGridView.EditingControl is and find a way to hook into every single change for every type of cell
Propagate the changes on every edit
Even then I think you still may run into issues because I'm not sure if DataGridView is designed to have cell values changed while a different one is being actively edited.

Categories