How do I set the value of a DataGridCell from code behind?

Oct 13, 2008 at 7:53 AM
Edited Oct 13, 2008 at 7:57 AM
I have a DataGridTextColumn in a DataGrid. I would like to manipulate the local value of the DataGridCell from code behind. I do not want to propagate the value to the property of the bound object. I want the actual commit of the value change to take place whenever IsEditing changes on the cell.

Here is my scenario. There's a field in which the user has to input a value that is dictated by a list of valid values. To aid the user, there's the possibility to popup a dialog in which the valid values are presented. The user chooses one of the valid values, and the value in the cell is changed accordingly. When the user tabs out of the cell, the value is commited.

The DataGridCell.Content gets the visual tree of the cell, either a TextBlock or a TextBox depending on whether the cell is in edit mode or not. I've tried to set the value on the TextBlock and on the TextBox (i.e. switched between IsEditing=True|False and set the value on the current FrameworkElement), but the local value of the cell stays unchanged.

Setting the new value to the DataGridCell.Content as proposed at:
doesn't work either, as would be expected.
Oct 14, 2008 at 1:19 PM
For your scenario, how is this different than the current behavior?  When tabbing out, that is when the cell is committed.
Oct 14, 2008 at 5:45 PM
Like I said, I would like to manipulate the local value of the cell, and let the user decide on whether to accept the proposed value that has been set from code behind.

To clarify, I want to know how to set the value of the cell as it would be set if the user typed it, but from code behind.
Oct 15, 2008 at 12:43 PM
Usually setting the value on the TextBlock of the DataGridCell.Content will do it.  If you were to try that normally without doing this special kind of editing, does it work?  I'm guessing that it could be the lost of focus due to your popup.  Anytime there is a lost of focus, the row will commit all it's data.  So maybe when the user selects the value and tabs they are already out of edit mode so it doesn't commit.  Let me know if this is the possible issue.
Oct 17, 2008 at 10:50 AM
Actually, as I explained in original post, I've tried setting the value in both the TextBlock.Text and TextBox.Text properties to no avail (both when the cell is IsEditing=false and IsEditing=true.

Here's a code example  to illuminate what I'm trying to do. The effect is that no value is propagated to my business object and no value is changed in the cell. There may be something obvious I'm missing here.



MyDialogWindow window = new MyDialogWindow();
bool? result = window.ShowDialog();
if (result.Value == true)
    cell.IsEditing =
    textBox = cell.Content
as TextBox;
    textBox.Text = window.ValueSelectedByTheUser;
Oct 21, 2008 at 12:41 PM
The issue is with the way you are entering edit mode.  Replace "cell.IsEditing = true;" with "MyDataGrid.BeginEdit();".