I'm using WPF XamDataGrid 23.2.157 in a MVVM project.
I want to use a XamDataGrid bound to a DataTable. The DataTable elements are just strings. The tables are dynamic (not known at compile time), and columns can be added/removed.
I want to be able to add columns, and then for each row in each column users select from a set of values (coming from the view model) in a combo box. Every cell should be bound to the same list of options.
I also want to be able to delete columns, preferrably with a delete 'x' in the column header.
I have started by handling FieldLayoutInitialized, but I'm wondering if there is a simple way to do this without handling events?
Hi Walter,
Sure, the XamDataGrid’s FieldSettings.DataItemUpdateTrigger property could be set to the OnCellValueChange enum value, which updates the associated’s Field data item as soon as a cell’s value is changed. In the case of a ComboBoxField, that would be selecting an item. I have modified the previous sample with this as well as a ColumnChanged event handler for the DataTable as a demo that the change is indeed performed.
Let me know if you have any other questions on the matter.
Best regards,Bozhidara Pachilova
6406.XDGDataTable3.zip
Bozhidara,
As a followup, it's working well so far. One behavior I'd like to add is to have the combo box commit when an item is selected. Currently I have to move to a different cell for it to be comitted and the new row updates, etc.
Is there and event or behavior I can handle to do that?
Thanks!
Thank you for clarifying.
While I understand the difference in this case, whether the fields are dynamically generated would be separate from the requirement about templating the label and showing/hiding/removing columns or creating ComboBoxFields, as far as I understand your scenario so far.
Anyways, I believe you wil find the “Define a Field Layout” documentation topic quite helpful in this case. There you can find snippets of programmatically defining fields for the XamDataGrid.
Doing this based on an unknown object, though, is a matter of coding it separately, for instance, a sample approach would be to iterate the DataTable’s columns and create fields according to their type and set with the column's name. Of course, the implementation for this would be subject to modifications on application level depending on the specific requirements and coding conventions.The fields can be ComboBoxFields as well and their ItemsSource can again be set to an IEnumerable of the local data context.
I have modified the previous sample to demonstrate a possible approach for this. Please, check it out and let me know if it accurately demonstrates what you’re trying to achieve. Alternatively, please, feel free to provide further details or modify the sample and send it back to me to communicate your ideas.
4863.XDGDataTable2.zip
Thank you for that sample. I probably didn't communicate this well, but my largest constraint is that I don't know the columns in advance, and they can be added and removed by the user. In your sample you are creating two predefined fields for Name and Country, but we don't have the ability to predefine them in code.