编辑单元格事件以获取单元格值和位置 [英] Edit cell event getting cell value and position

查看:95
本文介绍了编辑单元格事件以获取单元格值和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Datagrid中,我允许用户编辑一些单元格,在编辑完这些单元格后,我想将更改提交到数据库(在输入时)。我很难在网上找到资源来解决这个问题。

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.

我使用了一个事件:

CellEditEnding但是该事件仅向我提供行和列更改单元格的方式。我该如何使用它们来查找单元格本身并获取值?

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?

推荐答案

您可以使用此链接

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;
}

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

and call it from your event handler :

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;
        }

这篇关于编辑单元格事件以获取单元格值和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆