0

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 the cellEditEnding is, that it is only fired when I leave the editable cell.

Note: I am not using MVVM

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
user3367867
  • 565
  • 1
  • 7
  • 19
  • Would be nice, but is not. I checked it. There is only 1 datagrid und also the Bindings are checked and correct. Again: the datagrid behaviour is fine, but the values at the end of CellEditEnding .. are wrong. If you only see the datagrid in program, everything is fine – user3367867 Jan 03 '16 at 23:06
  • I don't follow. The duplicate suggests handling the event when the binding is updated. Have you tried that? – Charles Mager Jan 03 '16 at 23:08
  • sorry, i just don't understand your remark? I only fire the celleditending – user3367867 Jan 03 '16 at 23:11
  • As you've worked out, this isn't the point the binding is updated so handling this event isn't that useful for your purpose. The duplicate suggests two other approaches for getting the new value after it is changed. – Charles Mager Jan 03 '16 at 23:13
  • sorry, I first did not understand, what you meant with duplicate...I now run through the things, but no success. I found (sender as Datagrid).CommitEdit(); and then (sender as DataGrid).Items.Refresh() in several hints. But: in y environmant theItems.Refresh() is not allowed during execution (compiling is o.k) – user3367867 Jan 04 '16 at 00:11

0 Answers0