C# – Modifying DataGrid cell values on CellEditEnding event

cdatagriddatatablewpf

I've been trying to change a different cell value in same row inside CellEditEnding eventhandler in WPF DataGrid control.

If I edit the cell value in an existing row, it works as expected. However, when adding a new row, the change is not reflected to UI if another cell value is clicked by mouse without going into edit mode.

Moving focus to next cell by pressing tab also works as expected, but shouldn't it work for clicking aswell?

public MainWindow()
{
    InitializeComponent();

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(new DataColumn());
    dataTable.Columns.Add(new DataColumn());
    DataRow existingRow = dataTable.NewRow();
    dataTable.Rows.Add(existingRow);

    this.MyDataGrid.ItemsSource = dataTable.DefaultView;
    this.MyDataGrid.CellEditEnding += MyDataGrid_CellEditEnding;
}

void MyDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    (e.Row.Item as DataRowView).Row[1] = "a string that should be displayed immediatly";
}

So far I've tried to change binding UpdateSourceTrigger, NotifyOnSourceUpdated and NotifyOnTargetUpdated but nothing seems to work. I've also tried to do according fixing by reading related topics : http://codefluff.blogspot.fi/2010/05/commiting-bound-cell-changes.html

But those doesn't seem to help either..

One way that I've found to be working is following snippet:

var frameworkElement = this.MyDataGrid.Columns[1].GetCellContent(e.Row);
frameworkElement.SetValue(TextBlock.TextProperty, "a string that should be displayed immediatly");

but I wouldn't want to do dependency on dependencyproperties as I might not know the underlying display type.

So the question is that what could I do to achieve UI updation in new rows as it works with existing rows without using the provided second code snippet? I feel I am missing something..

Edit : Using ClipboardPaste will also work for setting new value but the original edited value will be lost for some reason.

Edit2 : The xaml is currently plain DataGrid, I've tried to do columns and their bindings manually but as I haven't got them to work I've removed them to keep the example code simple

<DataGrid x:Name="MyDataGrid" />

Edit3 : Added this image to describe the problem better. So after clicking next cell I would like the text to be displayed
Example image of the problem

Edit4 : Using snoop to view the cell seems to set the element visible, also calling .UpdateTarget(); for the binding seems to help, but it's not working automaticly. Still wondering what is the cause..

Edit5: I would like this to work with DataTable as I need it's other features

Best Answer

After testing huge amount of different kind of solutions I ended up adding the row when starting to edit a cell. As the feature I was seeking was working in existing rows it did fix the problem.

public class MyDataGrid : DataGrid
{
    protected override void OnBeginningEdit(DataGridBeginningEditEventArgs e)
    {
        base.OnBeginningEdit(e);
        this.CommitEdit();
    }

    protected override void OnCellEditEnding(DataGridCellEditEndingEventArgs e)
    {
        base.OnCellEditEnding(e);
        (e.Row.Item as DataRowView).Row[1] = "a string that should be displayed immediatly";
    }
}
Related Topic