I found a solution. The problem is the CellEditEnding-routine. It does not work. may be, because in CellEditEnding routine the ..Items.Refresh() statement does not work. So, I switched to the RowEditEnding-routine. There the Item.Refresh() works and also the update of the DataGid AND my DataContext-List. Nevertheless, thanks for your support, it lead me to the right ideas.
I tried hard and I did a lot of research to edit one column in the DataGrid, but without success. So I need some help.
I have declared a DataGrid with 5 columns in XAML with binding. The column to be edited is an integer. In the program, I read some data and add it to a list. The data is defined as a public class and the list is bound with DataContext.
This all works pretty fine, but directly after editing a cell I try to get the data and to execute some computations. I tried several triggers to get the data. The best one seems to be the datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
, because this is the only one, which is fired immediately after changing the data.
However:
The data is not up to date: I get the data back, as it was BEFORE the editing, not after the change. What could be wrong or what is the solution?
private void dgInvErfassen_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
if (row1.Item != null)
{
string nnn2 = row1.Item.GetType().Name;
}
var row_list = (Inventuren)dgInvErfassen.SelectedItem;
string db_ean1 = row_list.EAN_Nummer;
Again, what I tried and ask from my code:
- with DataGridRow
row1 = e.Row
I get the correct row in the DataGrid, BUT the data is the old one (BEFORE editing). - Trying it with the line
int row_index
etc. I get the row Items, BUT analysing the row in the included data search tool, the items also have the data BEFORE editing. - Trying it with
LostFocus
,LostKeyboardFocus
,SelectionChanged
and so on, I have the same problem and still is worse because they are all fired also when I click in any other cell. The advantage of thecellEditEnding
is, that it is only fired when I leave the editable cell.
Note: I am not using MVVM