【问题标题】:WPF DataGridRow not updating cell order after column sortWPF DataGridRow 在列排序后不更新单元格顺序
【发布时间】:2013-10-11 13:14:33
【问题描述】:

我有一个 DataGrid,在填充后我想将它导出为 Excel 格式。到目前为止,我能够做到这一点。当我尝试对列顺序进行排序时,我的问题就来了。标头是根据正确的顺序构建的,但 DataGridRows 不是。

图片显示最终结果:

在此示例中,我将“ID Equipa”列与“Tipo Entidade”交换,但是在 excel 文件(右侧)中,行值继续,就好像没有发生任何变化一样,而标题更新得很好。

不知道这是否有帮助,但我的“ExportToExcel”类基于此项目ExportToExcel Project,但没有使用它的类标识符

public class ExportToExcel<T, U>
where T : class
where U : List<T>
{
    // ...
}

我创造了这个

public class ExportToExcel
}
    public ExportToExcel(List<DataGridColumn> columns, List<DataGridRow> dataToFill)
        {
            // ...
        }
}

我认为问题出在我的“dataToFill”参数中,因为它保持默认的单元格顺序并且在列排序事件后不会更新。

我不明白为什么会这样。如果有人能对这个问题有所了解,我将不胜感激。

谢谢

编辑:

按照 Sheridan 的建议,我发布了一些额外的代码。

这就是我提取 DataGrid 行的方式

public IEnumerable<DataGridRow> GetDataGridRows()
    {
        var itemsSource = dgEntities.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = dgEntities.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (null != row) 
            {
                if (dgEntities.SelectedItems.Count == 0) yield return row;
                else if (row.IsSelected) yield return row;
            }
        }
    }

这就是我如何创建 ExportToExcel 类

public void ExportToExcel() 
    {
        if (dgEntities.ItemsSource != null)
        {
            try
            {
                BLL.ExportToExcel export = new ExportToExcel(dgEntities.Columns.ToList(), GetDataGridRows().ToList());
                export.GenerateReport();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }

下一点是我对前面提到的 CodeProject 项目中原始代码的覆盖

private object[] CreateHeader()
    {
        // Create an array for the headers and add it to the
        // worksheet starting at cell A1.
        List<object> objHeaders = new List<object>();
        foreach (DataGridColumn dgc in Columns)
        {
            if (dgc.Visibility == System.Windows.Visibility.Visible) objHeaders.Add(dgc.Header);
            else deletedColumns.Add(dgc.DisplayIndex);
        }

        var headerToAdd = objHeaders.ToArray();
        AddExcelRows("A1", 1, headerToAdd.Length, headerToAdd);
        SetHeaderStyle();

        return headerToAdd;
    }

    private void WriteData(object[] header)
    {
        object[,] objData = new object[DataToFill.Count, header.Length];

        for (int j = 0; j < DataToFill.Count; j++)
        {
            DataGridRow row = DataToFill[j];
            int i = 0;
            for (int x = 0; x < Columns.Count; x++)
            {
                if (!deletedColumns.Contains(x))
                {
                    DataGridCell cell = GetCell(row, j, x);
                    if (cell != null && cell.Content is TextBlock)
                    {
                        objData[j, i] = ((TextBlock)cell.Content).Text;
                        i++;
                    }
                }
            }
        }
        AddExcelRows("A2", DataToFill.Count, header.Length, objData);
        AutoFitColumns("A1", DataToFill.Count + 1, header.Length);
    }

【问题讨论】:

    标签: c# wpf sorting datagrid


    【解决方案1】:

    你真的不应该使用你不理解的代码......这就是使用你在互联网上找到的代码的问题。

    如果您查看CreateHeader 方法,您会看到标题标题来自该行:

    PropertyInfo[] headerInfo = typeof(T).GetProperties();
    

    这只是遍历您的类中定义的属性,与您的排序数据无关。要以简单的方式解决您的问题,只需重新排序 headerInfo 变量以匹配您的排序数据。

    但是,我建议您以更困难的方式解决您的问题,即了解您的代码在做什么,然后根据您的要求进行调整。

    更新>>>

    看到您正在遍历您的 DataGrid.ItemsSource 集合后,我猜这是您的问题,因为该集合在 UI 中排序后将保持不变。您有几个选择...一种方法是更新绑定到 ItemsSource 属性的实际集合,以响应用户对网格中各种标题的点击 - 这样,您当前的数据提取方法将起作用.

    也许更常用的方法是在您的收藏和DataGrid.ItemsSource...之间使用CollectionViewSource 对象...请参阅MSDN 上的CollectionViewSource Class 页面以获取有关此方面的帮助,包括XAML 示例。基本上,当使用此方法时,您可以从CollectionViewSource 对象中获取集合的副本处于其排序(和过滤)状态

    【讨论】:

    • 我从我的类中删除了该代码,并从头开始构建 CreateHeader 方法,它工作正常...问题不在标题上,而是在未根据 DataGrid 排序的行中标题...
    • 那么您的问题出在WriteData 方法中……在其中设置一个断点,看看实际发生了什么。
    • 可能,但是当我调试它时,值是默认的列顺序......所以我倾向于认为问题可能在于我如何在通过之前提取 List它到 ExportToExcel 类
    • 如果您向我们展示您是如何做到这一点的,那么有人可能会发现错误。
    • 刚刚发布了一些关键代码...感谢您的建议...希望我能得到帮助
    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2018-01-08
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2011-04-09
    相关资源
    最近更新 更多