WPF DataGrid ComboBox gets value before commit

I have a DataGrid in a WPF application (with Entity Framework) that has a ComboBox as one of the columns in it. This ComboBox bound to a data source that uses a federated link to a table containing the names displayed in the drop-down list. It uses the ID field (called SalesActMgrID) for this connection. I populate the drop-down list with just a list <> of specific names from this table.

My problem is that when Name is selected in the drop-down list, it changes the name in this joined table instead of changing SalesActMgrID to the selected name.

I figured out a way to update an identifier in my data source, but I did not understand a way to find out which name was selected in the drop-down list so that I could get the correct identifier for that name.

A combined column is defined as:

  <DataGridComboBoxColumn SelectedItemBinding="{Binding Path=ClientContract.StaffRole_SalesActMgr.StaffName}" Header="Sales Act Mgr" x:Name="salesActMgrColumn" Width="Auto" > <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding staffNamesListSAM}" /> <Setter Property="IsReadOnly" Value="True" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> 

The DataGrid is bound to the EmployeeTime data source. Full join tables:

 EmployeeTime.ClientContractID is joined to ClientContract.ClientContractID {M-1} StaffRoles.StaffRoleID is joined to ClientContract.SalesActMgrID {1-M} 

I use the following code to execute commits in a cell by editing the cell.

  private bool isManualEditCommit; private void consultantsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (!isManualEditCommit) { isManualEditCommit = true; string head = e.Column.Header.ToString(); bool doCommit = true; switch (head) { case "Sales Act Mgr": { e.Cancel = true; //This is where I have been able to 'hard code' in a different //ID value into the SalesActMgrID field which then correctly //updates the value, but I need to know what name was selected //here so I can get the correct ID for that name and set it below. ((EmployeeTime)e.EditingElement.DataContext).ClientContract.SalesActMgrID = 11; doCommit = false; } break; } DataGrid grid = (DataGrid)sender; if (doCommit) { grid.CommitEdit(DataGridEditingUnit.Row, doCommit); EmployeeTime et = e.Row.Item as EmployeeTime; CreateBurdenValue(et); } else { grid.CancelEdit(DataGridEditingUnit.Row); } isManualEditCommit = false; } } } 

There may be some other β€œbest” way to do this that I would like to know about. At least, if someone can point me in the direction in which I can get the chosen name, which was chosen only before any action is completed, I would be grateful.

Just for information, if I allow this to go through and make a normal CommitEdit in the cell, the selected name is actually updated in the StaffRole table, so in each grid line displaying the original name, they change to the new selected name (this is not what I want).

+4
source share
1 answer

I just summarized the fact that the OP is posted in a comment. To access the selected item in the editing handler, use:

  (e.EditingElement as ComboBox).SelectionBoxItem 
0
source

Source: https://habr.com/ru/post/1439436/


All Articles