0

In my Datagrid I allow users to edit some cells, after editing these cells I would like to commit the changes to the database (on enter). I am having troubles finding resources online to get this going.

I use an event called:

CellEditEnding but the event only provides me with the Row and Col how of the changed cell. How do I use these to find the cell itself and get the value?

4

1 回答 1

4

您可以使用此链接中的此功能

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[column]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        return cell;
    }
    return null;
}

并从您的事件处理程序中调用它:

object cellvalue = DataGridCell(yourgrid, e.Row, e.Column.DisplayIndex).GetValue;

这个函数也在函数内部被GetCell()调用

public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }
于 2013-01-17T21:20:03.117 回答