我可以在运行时在DevExpress的PivotGrid中对DateTime类型的数据进行分组吗?

马蒂亚斯·沃尔夫(Matthias Wolf)

我在WPF中有一个PivotGrid(DevExpress),并将数据源绑定到一个IList,该IList包含具有DateTime类型的多个属性的对象。我希望最终用户在运行时选择用户要按年,月或日分组的那些DateTime字段。那可能吗?

我知道我可以通过编程方式提供DateTime分组,但是由于有多个DateTime字段,因此如果最终用户可以选择要对哪个DateTime字段进行分组以及如何在运行时对其进行分组,则将非常繁琐和不必要。

你能指导我怎么做吗?

我有以下几点:

  <dxdo:LayoutControlItem ItemWidth="1*">
                                <dxpg:PivotGridControl MaxHeight="800" MaxWidth="800" DataSource="{Binding AllChildOrders}" DataSourceChanged="PivotGridControl_OnDataSourceChanged">
                            </dxpg:PivotGridControl>
                        </dxdo:LayoutControlItem>

并在后面的代码中:

private void PivotGridControl_OnDataSourceChanged(object sender, RoutedEventArgs e)
    {
        var pivotTable = sender as PivotGridControl;
        pivotTable.RetrieveFields();
    }

上面的代码有效,并且数据透视表将在运行时显示所有可用字段,包括类型为DateTime的字段。我不想以编程方式指定要以特定方式分组的字段,而是让最终用户在运行时选择如何分组的字段。可能的?

或者,我可以想象以编程方式创建子分组,如下所示:如何完成以下任务?

在此处输入图片说明

nempoBu4

0.预先生成的组

如果您不想以编程方式指定要分组的字段,则可以为每个DateTime字段预先生成组,以便用户可以在字段本身和字段组之间进行选择。
这是示例:

private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
    var pivotTable = sender as PivotGridControl;
    pivotTable.Groups.Clear();
    pivotTable.RetrieveFields();

    var dateTimeFields = pivotTable.Fields.Where(item => item.DataType == typeof(DateTime)).ToList();

    foreach (var field in dateTimeFields)
    {
        var group = new PivotGridGroup();
        group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (year)", GroupInterval = FieldGroupInterval.DateYear });
        group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (month)", GroupInterval = FieldGroupInterval.DateMonth });
        group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (day)", GroupInterval = FieldGroupInterval.DateDay });

        foreach (var groupField in group)
            pivotTable.Fields.Add(groupField);

        pivotTable.Groups.Add(group);
    }
}

这是示例的屏幕截图:
截屏

1.创建子分组

您可以使用PivotGridField.DisplayFolder属性创建子分组
这是示例:

private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
    var pivotTable = sender as PivotGridControl;
    pivotTable.RetrieveFields();

    var dateTimeFields = pivotTable.Fields.Where(item => item.DataType == typeof(DateTime)).ToList();

    foreach (var field in dateTimeFields)
    {
        var fieldYear = new PivotGridField()
        {
            FieldName = field.FieldName,
            Caption = field.Caption + " (year)",
            GroupInterval = FieldGroupInterval.DateYear,
            Visible = false,
            DisplayFolder = field.Caption
        };

        var fieldMonth = new PivotGridField()
        {
            FieldName = field.FieldName,
            Caption = field.Caption + " (month)",
            GroupInterval = FieldGroupInterval.DateMonth,
            Visible = false,
            DisplayFolder = field.Caption
        };

        var fieldDay = new PivotGridField()
        {
            FieldName = field.FieldName,
            Caption = field.Caption + " (day)",
            GroupInterval = FieldGroupInterval.DateDay,
            Visible = false,
            DisplayFolder = field.Caption
        };

        pivotTable.Fields.Add(fieldYear);
        pivotTable.Fields.Add(fieldMonth);
        pivotTable.Fields.Add(fieldDay);
    }
}

结果如下:
结果

2.自定义弹出菜单

您可以将命令添加到字段弹出菜单,该菜单允许用户更改组间隔。为此,您可以使用PivotGridControl.PopupMenuShowing事件和PopupMenuShowingEventArgs.Customizations属性来自定义菜单。
这是示例:

private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
    var pivotTable = sender as PivotGridControl;
    pivotTable.Groups.Clear();
    pivotTable.RetrieveFields();
}

private void PivotGridControl_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
    if (e.MenuType != PivotGridMenuType.Header)
        return;

    var fieldHeader = e.TargetElement as FieldHeader;

    if (fieldHeader == null)
        return;

    var field = fieldHeader.Content as PivotGridField;

    if (field == null || (field.Group != null && field.Group.IndexOf(field) > 0))
        return;

    var groupInterval = field.GroupInterval;

    if (groupInterval == FieldGroupInterval.Default && field.DataType != typeof(DateTime))
        return;

    var dateTimeIntervals = new List<FieldGroupInterval>(new FieldGroupInterval[]
    {
        FieldGroupInterval.DateYear,
        FieldGroupInterval.DateQuarter,
        FieldGroupInterval.DateMonth,
        FieldGroupInterval.DateDay,
        FieldGroupInterval.Hour,
        FieldGroupInterval.Minute,
        FieldGroupInterval.Second,
        FieldGroupInterval.DateWeekOfYear,
        FieldGroupInterval.DateWeekOfMonth,
        FieldGroupInterval.DateDayOfYear,
        FieldGroupInterval.DateDayOfWeek,
        FieldGroupInterval.Date,
        FieldGroupInterval.Default
    });

    if (!dateTimeIntervals.Contains(groupInterval))
        return;

    var pivotTable = sender as PivotGridControl;

    var subMenu = new BarSubItem() { };
    subMenu.Content = "Set group interval";

    if (field.Group == null)
    {
        var button = new BarButtonItem() { Content = "Year - Month - Date" };
        button.ItemClick += (s, eventArgs) =>
        {
            pivotTable.BeginUpdate();

            var group = field.Tag as PivotGridGroup;

            if (group == null)
            {
                if (groupInterval != FieldGroupInterval.Default)
                    field.Caption = field.Caption.Replace(" (" + groupInterval + ")", string.Empty);

                group = new PivotGridGroup();
                group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (year)", GroupInterval = FieldGroupInterval.DateYear, Tag = field, Area = field.Area, AreaIndex = field.AreaIndex });
                group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (month)", GroupInterval = FieldGroupInterval.DateMonth });
                group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (day)", GroupInterval = FieldGroupInterval.DateDay });

                foreach (var groupField in group)
                    pivotTable.Fields.Add(groupField);

                pivotTable.Groups.Add(group);

                group.Tag = field;
            }
            else
            {
                var yearField = group[0];

                yearField.Area = field.Area;
                yearField.AreaIndex = field.AreaIndex;
                yearField.ShowInCustomizationForm = true;
            }

            field.Visible = false;
            field.ShowInCustomizationForm = false;

            pivotTable.EndUpdate();
        };

        subMenu.Items.Add(button);
    }

    foreach (var dateTimeInterval in dateTimeIntervals.Where(item => item != groupInterval))
    {
        var button = new BarButtonItem() { Content = dateTimeInterval, Tag = field };
        subMenu.Items.Add(button);

        button.ItemClick += (s, eventArgs) =>
        {
            pivotTable.BeginUpdate();

            var group = field.Group;

            if (group != null)
            {
                var yearField = field;
                field = yearField.Tag as PivotGridField;

                field.Area = yearField.Area;
                field.AreaIndex = yearField.AreaIndex;
                field.ShowInCustomizationForm = true;

                yearField.Visible = false;
                yearField.ShowInCustomizationForm = false;
            }
            else if (groupInterval != FieldGroupInterval.Default)
                field.Caption = field.Caption.Replace(" (" + groupInterval + ")", string.Empty);

            field.GroupInterval = dateTimeInterval;

            if (dateTimeInterval != FieldGroupInterval.Default)
                field.Caption += " (" + dateTimeInterval + ")";

            pivotTable.EndUpdate();
        };
    }

    e.Customizations.Add(subMenu);
}

结果如下:
结果与子菜单

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以在运行时检查内置类型吗?

来自分类Dev

我可以在C ++中在运行时更改对象的类型吗

来自分类Dev

我可以在运行时更新 AMQP 设置吗?

来自分类Dev

Java:我可以在运行时将运行时异常注入到任意类方法中吗?

来自分类Dev

我可以在运行时指定动态数据源吗?

来自分类Dev

我可以在运行时指定动态数据源吗?

来自分类Dev

是否可以在运行时在Julia中创建类型?

来自分类Dev

是否可以在运行时在Typescript中验证类型?

来自分类Dev

我可以在运行时在活动的Python Shell中更改模块的属性吗?

来自分类Dev

我可以在运行时在Ormlite中构建自定义查询吗?

来自分类Dev

我可以在运行时在活动的Python Shell中更改模块的属性吗?

来自分类Dev

C#8中的不可为空的引用类型在运行时可以为null吗?

来自分类Dev

我们可以在运行时在 C# 中为运行时创建的控件创建多个事件吗

来自分类Dev

在 Python 中,我可以在运行时更改全局变量后在函数中传递它吗?

来自分类Dev

根据用户在运行时和时间间隔输入的字段对数据帧进行分组

来自分类Dev

C ++是静态类型语言,为什么我们可以在运行时获取类型

来自分类Dev

我可以在运行时运行C#文件吗

来自分类Dev

我可以在.NET Core可执行文件中嵌入文件并在运行时读取它吗?

来自分类Dev

我可以在运行时在Swift中从枚举名称和值的rawValue实例化或产生枚举值吗?

来自分类Dev

DevExpress WebChartControl在运行时创建

来自分类Dev

您可以在运行时检查对象是否符合Flow类型吗?

来自分类Dev

在运行时通过UITableView中的属性将项目分组的任何方法吗?

来自分类Dev

Android / Google Play-应用程序可以在运行时进行更新吗

来自分类Dev

我可以在运行时将std :: vector转换为std :: tuple吗?

来自分类Dev

我们可以在运行时为表单加载dfm文件吗?

来自分类Dev

我可以在运行时通过评估字符串来创建函数吗?

来自分类Dev

我们可以在运行时检查动态数组的大小吗

来自分类Dev

我可以在运行时使用C#动态构建函数吗?

来自分类Dev

我可以在运行时通过评估字符串来创建函数吗?

Related 相关文章

  1. 1

    我可以在运行时检查内置类型吗?

  2. 2

    我可以在C ++中在运行时更改对象的类型吗

  3. 3

    我可以在运行时更新 AMQP 设置吗?

  4. 4

    Java:我可以在运行时将运行时异常注入到任意类方法中吗?

  5. 5

    我可以在运行时指定动态数据源吗?

  6. 6

    我可以在运行时指定动态数据源吗?

  7. 7

    是否可以在运行时在Julia中创建类型?

  8. 8

    是否可以在运行时在Typescript中验证类型?

  9. 9

    我可以在运行时在活动的Python Shell中更改模块的属性吗?

  10. 10

    我可以在运行时在Ormlite中构建自定义查询吗?

  11. 11

    我可以在运行时在活动的Python Shell中更改模块的属性吗?

  12. 12

    C#8中的不可为空的引用类型在运行时可以为null吗?

  13. 13

    我们可以在运行时在 C# 中为运行时创建的控件创建多个事件吗

  14. 14

    在 Python 中,我可以在运行时更改全局变量后在函数中传递它吗?

  15. 15

    根据用户在运行时和时间间隔输入的字段对数据帧进行分组

  16. 16

    C ++是静态类型语言,为什么我们可以在运行时获取类型

  17. 17

    我可以在运行时运行C#文件吗

  18. 18

    我可以在.NET Core可执行文件中嵌入文件并在运行时读取它吗?

  19. 19

    我可以在运行时在Swift中从枚举名称和值的rawValue实例化或产生枚举值吗?

  20. 20

    DevExpress WebChartControl在运行时创建

  21. 21

    您可以在运行时检查对象是否符合Flow类型吗?

  22. 22

    在运行时通过UITableView中的属性将项目分组的任何方法吗?

  23. 23

    Android / Google Play-应用程序可以在运行时进行更新吗

  24. 24

    我可以在运行时将std :: vector转换为std :: tuple吗?

  25. 25

    我们可以在运行时为表单加载dfm文件吗?

  26. 26

    我可以在运行时通过评估字符串来创建函数吗?

  27. 27

    我们可以在运行时检查动态数组的大小吗

  28. 28

    我可以在运行时使用C#动态构建函数吗?

  29. 29

    我可以在运行时通过评估字符串来创建函数吗?

热门标签

归档