Datagrid/editing Cell/column

Dec 4, 2008 at 3:06 PM

I want to update datasource after editing cell in code behind, my question is how to do it? 



cell.IsEditing = true;
cell.Content =

"new content";



Visualy it seems to work, but when i click in the cell the old value is still there.
My issue is tak a inserted value in one cell check this value to database if it exist, if exist then use another value from the database and place it in next cell, then move to this cell and set it to editmode.

Dec 4, 2008 at 3:22 PM

To solve the problem I think you need to tell us a little more about how you are using the DataGrid. Are you using data binding?

A simple solution would be to create a typed dataset from your database then bind the Datagrid to the DataTable. Any changes to the data via the teh DataGrid will make it into your DataTable, also you can change your DataTable cell values programmatically and the DatGrid will update.

I describe this, and some more advanced layered patterns in this article:

If you have other questions - please provide a bit more detail and more code snippets.

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples

Dec 4, 2008 at 3:33 PM
In this case i use LINQ to SQLClasses:


var i = from c in db.SalesOrderLines



where c.SalesOrderID == 1



select c;


dgOrderLine.ItemsSource = i;

I have also try to use this code in CellEditEnding, but the textblock dont get the value from cell, so i get a NULL value.




row = e.Row;



DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);



DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(1); // get fac value cell



TextBlock textBlock = cell.Content as TextBlock;




BindingExpression be = textBlock.GetBindingExpression(TextBlock.TextProperty);



Kindly help

Dec 4, 2008 at 3:41 PM
Can I just confirm what you are wanting to do ...

What you want is that when the user finishes editing a single cell in the DataGrid, that the bound data is updated to reflect this? However, currently you will not see the updated value from that cell (or any others in that row) until the row edit ends and the values are committed.

Is that correct?

The CellEditEnding event will not give you any joy - it occurs before the value from the editing control is written back to your bound data object - as I guess you have found.

Do your objects implement IEditableObject? if so - then after each cell edit you need to commit the changes, this can be done by invoking the EndEdit() method on your data objects each time the user moves between the different cell values.

Colin E.
Dec 4, 2008 at 6:57 PM
Edited Dec 10, 2008 at 9:11 AM

My issue is to update another cell than is in editing and focus. The changes like "cell.Content = "new content";" is visible  in the current cell, but when I move to this cell and click, the old value "Test" is visible and "new content"is gone. I have some line of code that moves me to next cell in the same row:



void MoveNext(KeyEventArgs e)





bool previous = ((e.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift);
// Start navigation from the current focus to allow moving focus on other focusable elements inside the cell
UIElement startElement = Keyboard.FocusedElement as UIElement;
ContentElement startContentElement = (startElement == null) ? Keyboard.FocusedElement as ContentElement : null;
if ((startElement != null) || (startContentElement != null))
e.Handled =
FocusNavigationDirection direction = FocusNavigationDirection.Next; //previous ? FocusNavigationDirection.Up : FocusNavigationDirection.Down;
TraversalRequest request = new TraversalRequest(direction);
request.Wrapped =
true; // Navigate only within datagrid
// Move focus to the the next or previous tab stop.
if (((startElement != null) && startElement.MoveFocus(request)) ||
((startContentElement !=
null) && startContentElement.MoveFocus(request)))
UIElement currentElement = Keyboard.FocusedElement as UIElement;
DataGridRow currentRow = FindVisualParent<DataGridRow>(currentElement);
// if (currentRow != null) currentRow.IsSelected = true;
And this code make changes back to the underlaying bound data, when the user moves to next cell.
I have not implement IEditableObjects.

Do you understand what I thinking?

Dec 5, 2008 at 6:31 AM
It sounds to me like what you want can be most easily accomplished by implementing INotifyPropertyChanged on the underlying object, and updating that rather than the cell directly. dataGrid.CurrentItem seems like it could be very handy in your case.