i am using the EF and the MVVM approach to get a bunch of data records in an ObservableCollection. This ObservableCollection lives in a ViewModel.
The ViewModel is than bound to a DataGrid. This works just fine.
For the sake of data consistency i now want to turn a bunch of columns in the DataGrid into ComboBoxes. So the user can only choose from a predefined set of values. These predefined values are also stored in the database (You can think of it as a lookup table). I can load the values from the database and also put it in an ObservableCollection. But now i wonder how can i bind this to the DataGrid, since i can only specify one ItemSource for the DataGrid.
Is this even the right approach in WPF?
Bind the ItemsSource property of DataGridComboBoxColumn to your values from the lookup table.
Then bind SelectedValueBinding to the field in the main collection.
If you are using normalised values (i.e. numeric values mapped to the items in the lookup table), then you should also set the SelectedValuePath and DisplayMemberPath properties.
Related
I encountered a very specific behave on the Comboboxes in Winform. I found a lot of quite similar questions, but no useful anwser to my problem:
I have a form where controls are bound to a SQL datasource via TableAdapter/manager. Some of the values are linked to Lookuptables which are of type Combobox. When I first fill the base dataset, all is shown correctly. I can choose items from Combobox (fields id and NamedItem) where "id" is bound as datamember to the base datset table and "NamedItem" is the DisplayMember of the Combobox.
No problems when changing values or updating but ...
once I have to refill the Combobox underlaying datasource (eg. in case, an entry was added), the selected Index of the Combobox is set to 0 - means, the first item in the list.
Sure, I could remember the selectedindex and restore it after. For one Combobox, no problem, but for dozens ...
Is there any other way to keep the last selecteditem after refilling the combobox?
If items would not be deleted from your combo list , turning the Clearbeforfill property of your Table Adapter to False can prevent that.
I'm developing my first WPF application that queries a database and shows some records of some tables in TabControl if one or more fields of these records not satisfy certain condition.
I have a DataTable as data source and I use a DataGrid to show results (i.e. the wrong records). I'd like to use ToolTip on DataGridCell to indicate why a field is considered wrong. There's a way to iterate over the DataGridRow and the DataGridCell so that I can set dynamic ToolTipfor every specific field?
Thanks in advance.
I would bind the DataGrid selected item to a SelectedRecord property in my view model (where the data source is coming from), see Get selected row item in DataGrid WPF as an example. The SelectedRecord property would then set the SelectedRecordToolTip property in accordance to the SelectedRecord value (i.e. using a dictionary with the error as the key and the tooltip as the value). Finally, you can then bind your tooltip to the SelectedRecordToolTip property.
My setup:
C#.Net 4.0, Windows Forms, DevExpress 13.1.5 although I doubt its a DX issue
I have a form with a GridControl (with GridView) on the top and a detail area that holds TextEdits and other edits in a LayoutControl below.
Both the grid and the edits below are bound to the properties of the objects contained in a list in the binding source.
The grid is set to ReadOnly, MultiSelect, RowSelect and all its columns are set to ReadOnly, not focusable.
Editing is only happening in the detail area below.
The behavior I want to create:
When multiple rows are selected in the grid the edits below should show the following:
the value of field in question is the same in all selected rows -> show the value
the value of the field in question differs between the rows -> show nothing
if the user writes into the TextEdit while multiple rows are selected:
the value of the edit should update the values of the same field of all the selected rows
Where I am with this:
I'm working on a solution by building a custom BindingSource that would be aware of the selection. It would bind the list of object to the grid and a single object that is not part of the list to the edits. Depending on the selection I would set the properties of that single object or forward its changes to the selected objects in the list.
I got that working for a single property with 2 binding sources and would now extend it to use reflection to do this for all of the public properties. I also want to encapsulate the whole behavior into a class that looks and acts like BindingSource just with that added behavior.
The question:
Is there a simpler way to achieve this? Does something already exist which can do this that I overlooked in either .Net or DevExpress? Are there traps to my approach that I should consider or why I should go about this totally differently?
I think that you can achieve your goal in a simpler manner:
Just bind a single BindingSource with all the data that you need to your grid. That should display the data.
Then, bind the required fields from that same BS do the edits through the DataBindings propriety.
You can then implement a save object (through a control or programatically) so the changes made in the edits are shown in the grid.
To check the grid values, you can use:
//get the handles of the rows
gridView.GetSelectedRows();
//get the value of the desirable cells
gridView.GetRowCellValue(handle, column);
Also, in future projects, consider using the Entity Framework to construct a data-aware model and custom objects based on the elements of your Database.
Hope this helped!
I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
You can set ItemsSource and DisplayMemberPath properties:
comboBox1.ItemsSource = dataGrid1.ItemsSource;
comboBox1.DisplayMemberPath = "ColumnName";
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.
I want to know how you can modify properties of columns of a WPF toolkit datagrid once the ItemsSource has been set (it is set in XAML)? For example I want to make a specific column have the property IsReadOnly equal to true.
So basically there are two things I want to know:
-How can I get access to a specific column once the ItemsSource has been
set?
-How can I change the properties of a specific column once the ItemsSource
has been set?
Answer to both questions is:
By looping through your datagrid
Access column by it's index