【问题标题】:Get all cells in datagrid获取数据网格中的所有单元格
【发布时间】:2012-07-23 14:44:09
【问题描述】:

有没有办法获取 DataGrid 中所有单元格的可迭代集合,无论它们是否被选中

【问题讨论】:

标签: wpf wpfdatagrid


【解决方案1】:

如果您的意思是 DataGridCells,您可以使用 Vincent Sibals 辅助函数来遍历所有行 DataGrid.Items 和列 DataGrid.Columns

public DataGridCell GetCell(int row, int column)
{
    DataGridRow rowContainer = GetRow(row);

    if (rowContainer != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

        // try to get the cell but it may possibly be virtualized
        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        if (cell == null)
        {
            // now try to bring into view and retreive the cell
            DataGrid_Standard.ScrollIntoView(rowContainer, DataGrid_Standard.Columns[column]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        }
        return cell;
    }
    return null;
}

public DataGridRow GetRow(int index)
{
    DataGridRow row = (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // may be virtualized, bring into view and try again
        DataGrid_Standard.ScrollIntoView(DataGrid_Standard.Items[index]);
        row = (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

编辑

如果 grid 是您的 DataGrid,您将获得所有 DataGridCells 的列表,如下所示:

List<DataGridCell> allCellList = new List<DataGridCell>();

for (int i = 0; i < grid.Items.Count; i++)
{
    for (int j = 0; j < grid.Columns.Count; j++)
    {
        allCellList.Add(grid.GetCell(i, j));
    }
}

【讨论】:

  • GetRow 有效,但在 GetCell 方法中,编译器不允许我将 GetVisualChild 与类型参数一起使用,并且只接受整数作为参数。
  • for (int i = 0; i &lt; grid.Columns.Count; i++)
  • 是的,这条线无法正常工作DataGridCellsPresenter presenter = GetVisualChild&lt;DataGridCellsPresenter&gt;(rowContainer);
  • 我是说上面的 GetCell 方法不起作用,因为它需要使用不允许的 DataGridRow 获取 DataGridCells 演示者,您必须使用行索引并将返回的值转换为一个 DataGridCells Presenter `DataGridCellsPresenter presenter = (DataGridCellsPResenter) GetVisualChild(row);` 像这样,否则它不会编译,这会导致异常,所以我不太确定你告诉我用这个方法做什么。跨度>
  • 我已经编辑了我的答案,向您展示如何获取所有 DataGridCells 的列表。我不明白您对 DataGridCellsPresenter 的要求。对不起。
【解决方案2】:

为了方便(不一定是性能),您可以将 DataGrid 中的数据(包括所有列和行的所有单元格)填充到单个 DataTable ,它提供了一些函数来帮助操作你的数据,例如迭代、过滤、排序等。

// Populate a DataGrid to a DataTable
DataTable dt; 
DataView dv = (DataView) myDataGrid.DataSource;
dt = dv.Table.DataSet.Tables[0];

您随后可以使用泛型将任何特定列转换为集合或列表,只需一行代码。见how-do-you-convert-a-datatable-into-a-generic-list

List<DataRow> myList = dt.Rows.Cast<DataRow>().ToList();

它使您免于编写循环。

【讨论】:

  • 其实你在做相反的事情,你没有回答问题。
  • 我对OP的理解是将所有行和列的所有单元格存储到一个可以方便操作的对象中。 DataTable 允许您选择任何数据列并使用泛型将它们转换为集合。如果我错了,请纠正我以及为什么。
  • 我同意直接操作/迭代数据而不是 DataGridCells 更好。但是您将数据从 DataTable 传输到 DataGrid 中,我认为 OP 是在问另一个方向。
  • 你是对的@LPL。我有两次没有看到我正在做相反的事情。我已经编辑了我的答案。
  • 这是围绕数据迭代还是实际上围绕单元格本身进行迭代?我希望能够自己访问控件。虽然如果这是围绕数据,它在其他地方对我仍然有用。
【解决方案3】:

修复线路抛出的错误...

    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter> 
        (rowContainer);

添加此例程:

private T GetVisualChild<T>(DataGridRow rowContainer)
    {
        throw new NotImplementedException();
    }

【讨论】:

  • 这并不是问题的真正答案。这是修复另一个答案中的问题的评论吗?
  • 是的,泰尔先生。我试图用绿色复选标记传达解决方案下其中一个 cmets 中出现的错误的修复。
  • 我已经提交了一个编辑请求,我试图在其中明确说明
  • 谢谢托拜厄斯。下次我会尽量模糊一点! :)
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
相关资源
最近更新 更多