RowValidating doesn't prevent leaving an invalid row - winforms

I have bunch of textboxes bound to a datagridview. I want to prevent users from leaving a row if invalid input is entered.
I tried both the RowLeave and RowValidating events. They both give me the data row user is entering, not the old one. Therefore, in the RowValidating events, when I set the event.cancel=true, it does not revert back to the old row.
Basically I need a way to capture the row the user is trying to leave, and I am not able to do it with any of the those events.
Thanks!!
Edit : I guess a silly way to do it is via rowEnter event. I imagine I can accomplish what I want with that, but all the purpose of those fancy events are lost...
Update : I think the problem is with the data binding. I can see the underlying datarow is locked but that is not reflected in the textboxes. Yikes..

Try CellValidating, I know this used to work for me.

Related

Wpf update source trigger explicit on multiple textboxes when a button is clicked and command is executed. In MVVM scenario

I have a situation like this, I have a browse when I can double click an object and it opens in a form and then I am able to modify it (behind the scenes I am passing that object from one view model to another). Then I am able to modify its fields, however the textblock does it immediately, so I can see the fields changing before I press save changes, whats worse when I press cancel the modifications stay on the browse.
I know I have to tell all the textboxes to UpdateSourceTrigger explicit, but I cannot find a simple example in an MVVM way, so only update when the button save is clicked and command associated with it is executed, that should force that explicit update on all texboxes. Any ideas how can I achieve it?
Thanks :)
You use magic. Or, you send a copy of the model, then wait for an OK, and on OK copy the values back to the original. Then you do a little dance and drink a little water.

How to cancel datagrid edit

I have a view that uses a usercontrol that contains a datagrid. The requirements for the view state that if a user deletes the value out of the "Customer Name" column then leaves the cell, don't commit the edit and change the value back to its original value - i.e. don't allow blank customer names. The usercontrol is shared code and is used between multiple applications. The edit cancellation requirement is specifically for the view mentioned above (not all applications that use the usercontrol). How do i detect the value for the Customer Name cell has been deleted and cancel the edit if the value is empty?
You can handle CellEditEnding - its Occurs before a cell edit is committed or canceled. Here you can validate current value of the cell, and if it satisfy your condition[s] you can do what ever you want. MSDN
Edit:
Its my suggestions, but I can't approve it.
You can get your new value via e.Row.Item, and cast it to object you put on DataGrid.
Another way is cast sender object right way to get access for new cell value.
+1 is to DataGridCellEditEndingEventArgs.EditingElement. Its FrameworkElement, and I think you know what it is (probably TextBox, or something else).
also I think you know what you should to do!

When Should I Retrieve Values from Textbox?

Suppose I have a Window with TextBoxes I want to use the values. Right now I'm thinking of either:
1) Updating each associated value once the cursor is out of focus, and once the user presses Ok I start the program
2) Once the user presses Ok, I retrieve all the values at once then start the program
I'm not sure which one is better though. First alternative seems more modular, but there's more semantic coupling since I each new box is supposed to be updating its respective value.
I realize this isn't all that important, but I'm trying to understand when to centralize and when not to. Other better approachers are appreciated too.
Use data binding to bind the text boxes' contents to objects in your code behind. WPF will take care of updating your attributes. Usually updating the data-bound value is done when the focus is lost on text boxes. However, you can also specify that it will happen whenever the value changes.

In Silverlight 3 can every cell of the datagrid be editable concurrently?

In an SL3 datagrid, is it possible to have every cell of the grid editable?
I need to create a UI that's similar to an Excel worksheet. Upon a button click, the entire collection of objects would be submitted as opposed a single object or cell.
Is this even possible, and if so how would I go about achieving it?
Thanks.
I guess the reason why this question has sat here 4 hours with no answer is that we all asking ourselves "Have I missed the problem here?".
First of all you can't actually "edit every cell concurrently", after all when you hit a key on the keyboard only one control is going accept that is input, the one with the focus.
"Excel worksheet" behaviour is exactly what you get from DataGrid if you let it automatically generate the cells.
So this question is really about the object you assign to the ItemsSource property. You really need to tell us about what you are using to store the data.
The truth is your requirement is quite easy to deliver especially if you include WCF RIA services. Ulitmately you get a "Data Context" which you can edit in various ways and then submit changes at whatever point makes sense for your application.

Winforms using BackgroundWorker to cycle through Datatable

Please could you give me your thoughts on the following (especially if its advisable or not to do so)...
Basically, I can successfully import CSV data into a datatable and then bind that datatable to a datagridview. What I would like to do now is run through some validation checks for each row in the grid. Each row will have its data validated thru a Stored Procedure that will do a bunch of checks and return one of two values. I would then like to display this value in the last column (originally empty) of the grid, and then move on to the next row and repeat this action until all rows have been validated.
I hope this paints a clear picture of my intentions. In order to update the UI I would have to use the BackgroundWorker component, but am just concerned that this may not be the right way to go about it, and especially how to go about it.
Please advise me. Thank u!
For a long operation, a background worker is the best way to perform a long task without making the GUI freeze.
You can use the worker's event ProgressChanged event to update the DataGrid. Note that you will have to update the DataGrid using the Invoke method, since GUI must be updated from the correct thread and Invoke transfers your action from the BG's thread to the GUI's thread.

Resources