【问题标题】:Edit cell event getting cell value and position编辑单元格事件获取单元格值和位置
【发布时间】:2013-01-17 21:14:52
【问题描述】:

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

我使用了一个名为:

CellEditEnding 但该事件仅向我提供了更改单元格的 Row 和 Col 方式。如何使用这些来查找单元格本身并获取值?

【问题讨论】:

    标签: c# .net wpf datagrid


    【解决方案1】:

    你可以使用这个link中的函数

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

    【讨论】:

    • 错误 1 ​​非泛型方法 'System.Windows.FrameworkElement.GetVisualChild(int)' 不能与类型参数一起使用
    • 别忘了包含这个命名空间using System.Windows.Controls.Primitives;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多