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

Can I group DateTime type data in PivotGrid of DevExpress during runtime?

我在 WPF 中有一个 PivotGrid (DevExpress),并将 DataSource 绑定到一个 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();
    }

以上代码有效,枢轴 table 在运行时显示所有可用字段,包括 DateTime 类型的字段。我不想以编程方式指定以特定方式对哪些字段进行分组,而是让最终用户在运行时选择分组方式和字段。可能吗?

或者,我可以想象以编程方式创建子分组,如下所示:我怎样才能完成以下操作?

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.创建子组ins

您可以使用 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 event and 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);
}

这是结果: