0
votes

I have a little bit of a quandary that I haven't been able to resolve, at least not in a method that makes sense from an MVVM standpoint.

I have a datagrid that contains client-employee data with start and end dates for each client/employee relationship.

The DataGrid overall ItemsSource is the ClientToEmp CollectionViewSource bound to the ClientToEmpObservableCollection. However, in the combobox column that allows them to change/update the current employee the ItemsSource is the Employee CollectionViewSource bound to the User ObservableCollection(ie, a list of all employees they can choose for this client).

This part works fine, when I click the combobox, the proper employees are displayed to select from in the combobox. However when the datagrid loads, I want the CurrentEmp from the ClientToEmp cvs to be showing as the selected employee(ie, the employee that is currently assigned to this client). When they click on it, they should be able to change the Employee(from the separate employee cvs), which would then update the value in the ClientToEmp cvs.

<DataGrid Name="ClientToEmpMDG" ItemsSource="{Binding cvsClientToEmp}" 
                      AutoGenerateColumns="False" AutoGeneratingColumn="Gen_AutoGridColumns">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" IsReadOnly="True"/>
                    <DataGridTemplateColumn Header="Current Emp">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
                                          DisplayMemberPath="DisplayName"
                                          SelectedValuePath="User_ID"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Start Date">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker Name="StartDateDP" SelectedDate="{Binding Path=Start_Date}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="End Date">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker Name="EndDateDP" SelectedDate="{Binding Path=End_Date}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

How can I get the current employee to show properly on load? The selected value should match the User_ID from the two ObservableCollections. If I use the SelectedValue Property, all comboboxes in the grid show the same value, and it still doesn't bind properly to the cvsClientToEmp value I want it to bind to. I want each combobox to have it's own value, not all of them to share a single value.

UPDATE: I fixed the issue with all of the comboboxes displaying the same value by changing adding "IsSynchronizedWithCurrentItem" to False...Still cannot get it to bind to the value coming back from the DB as to who the current employee is

1
I am having a little trouble following along. Could you possibly add a picture of what you mean?Selthien
@Selthien I can't add a picture but basically I have a DataGrid where I am loading data from the server into an ObservableCollection populating Client To Employee relationships. User_ID, CurrentEmp, ClientName, StartDate, EndDate are the fields. The column "Current Emp" is a combobox column, with combobox items of all employees(the items source is different from the DataGrid). I want each row to automatically select the CurrentEmp in the ClientToEmp ItemsSource the datagrid is using as the selected value(ie, display this in the combobox when it loads). I cannot get this working.MattE
@MattE Of which type is "CurrentEmp"? Same as "User_ID" of an item from your ComboBox-Source?nosale

1 Answers

0
votes

You are missing SelectedValue in your ComboBox

<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValue="{Binding CurrentEmp}"
    SelectedValuePath="User_ID"/>

See also https://stackoverflow.com/a/4902454/10718884 for a good explanation


Note: There is also a DataGridComboBoxColumn

<DataGridComboBoxColumn
    ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValueBinding="{Binding CurrentEmp}"
    SelectedValuePath="User_ID">
</DataGridComboBoxColumn>