How to cancel datagrid edit - wpf

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!

Related

WPF Data gird row selection change event from selection change event

I'm looking for a workaround to an issue I've noticed in a project that I'm working on. I have a datagrid bound to a list of a class I've created. Whenever a user types in a stock symbol in this datagrid I have the roweditending event set to pull in some information to populate the other fields in the datagrid.
This generally works fine, however I've noticed that the event doesn't fire in some instances. I'm going to try to explain this the best I can... upon data entry if I enter information into a cell and then click another row on the datagrid - focus doesn't shift to the new row but shifts to the row containing the cell I was just entering data into from that cell, then if I select another row or input element the row edit ending event doesn't appear to fire and as such my code to populate the rest of the row doesn't execute.
Everything else is fine, and it's a pretty subtle issue, only one path of input to make it happen, but I was wondering if anyone had run into this or found a work-around?
I've been looking at the selection change event, but I need to be able to grab the row that was selected. In the row edit ending event I use
Dim NewTrade As Trade = e.Row.DataContext
to get the instance of my class that the row represents and to add the additional fields.
I'm not sure I see a way to get the row and it's datacontext for the row I just shifted from using the SelectionChangedEventArgs.
Any ideas for a workaround?

DataGrid selected item changed more quickly than data loads from DB

We have the following WPF : a datagrid with row detail template. Selected line on this grid (customer selection) is handled to trigger two queries to retrieve address and contacts data, then row detail template show these data in two tabs, each with a datagrid. All the magic is done with binding, subgrids bind to properties of main Customer object, which we have as an IObservableCollection.
The bug happens when using “move down” arrow on keyboard, selected index changes fast, so fast than queries result “come back late” to interface, thus data is incoherent to what is shown and data update cannot perform. We have an exception. So my question is : how to prevent this in a proper manner ?
Should we have a try-catch the right kind of obscure exception then do nothing (loose data that cannot be applied to interface) ?
We don’t want to wait for data to come back, if user scrolls very quickly, customer selected line should go down and no matter details aren’t shown.
Maybe we should have a delay before selected item details are retrieved ? Thus no query if selected item changes before end of delay ?
Thank you for your ideas.
Gists for code:
Xaml : https://gist.github.com/Xarkam/3b89eb614124bb2f2307
Selected index changed handling : https://gist.github.com/Xarkam/cf28844ce05fd4984807
Edit 10th of July :
I have modified the main datagrid items definition as in following gist : (add https:// prefix, I don't have enough reputation to add more links, sorry) gist.github.com/postb99/d3be79f0ef2544d685f9 (inspired from stackoverflow.com/questions/13374957/datagrid-throws-invalidoperationexception-by-scrolling and proposed answer) but problem still persists...
We've solved it, if I remember well we didn't have observable collections for every collection of objects to display.

Binding to Dictionary/Array of objects

I have large collection of statuses(bool) that are reached by key (address)
the visual should display each status as different control (for example checkboxes, buttons, radios , etc) - each control is provided with the address of the status it will display
for example
button1 <- status[55]
checkbox1 <- status[81]
..
etc
my question is if i put INotifyPropertyChanged on whole indexer(if i do it with indexer) - if one value changes does it update all the controls or only the changed one..
I want only one status change to update only one control - not all of them. Is there a way doing this?
It will update all, in Silverlight you could construct a notification that only updates one index. I cannot think of any solution which would let you keep that structure, if you map everything to objects with key and value you could internally notify of value changes...

RowValidating doesn't prevent leaving an invalid row

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.

Detect changes to user input controls in Silverlight?

I have a childwindow with a number of Textboxes, Comboboxes, and DatePickers. I want to know if a user has changed any value in these (to know if I need to save to db)
One way I could think of doing this are in the 'on chg' event handlers and set bool. But if a user changes the value, in say a combobox, then changes back to the original this would still be seen as a change.
Are there other alternatives?
(note the project is not set up as MVVM)
If you don't use mvvm but still bind to an object then:
before the window is shown create a copy of the object, save it, and bind it to DataContext
whenever you need to know if user made any changes you can compare the saved object to DataContext (property by property)
I you don't use binding at all then:
before the window is shown save all fields that can be modified to a Dictionary
whenever you need to know if user made any changes you can compare the dictionary values to values of the fields

Resources