【发布时间】:2013-01-17 21:14:52
【问题描述】:
在我的 Datagrid 中,我允许用户编辑一些单元格,在编辑完这些单元格后,我想将更改提交到数据库(输入时)。我很难在网上找到资源来完成这项工作。
我使用了一个名为:
CellEditEnding 但该事件仅向我提供了更改单元格的 Row 和 Col 方式。如何使用这些来查找单元格本身并获取值?
【问题讨论】:
在我的 Datagrid 中,我允许用户编辑一些单元格,在编辑完这些单元格后,我想将更改提交到数据库(输入时)。我很难在网上找到资源来完成这项工作。
我使用了一个名为:
CellEditEnding 但该事件仅向我提供了更改单元格的 Row 和 Col 方式。如何使用这些来查找单元格本身并获取值?
【问题讨论】:
你可以使用这个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;
}
【讨论】:
using System.Windows.Controls.Primitives;