前言
在EF架构中使用PivotGridControl控件要绑定数据源时,每一次都要去后台查询出需要的数据集,然后甄选出我们需要的用以pivotgridcontrol所绑定的数据源的字段,接着定义一个个的PivotGridField,最终添加到一个List<PivotGridField>集合中作为数据源进行绑定。
其次,对于PivotGridControl也要去创建设置一些显示列也会有一些繁琐。
当我一个系统中有多处需要使用这个控件的时候,那将严重的花费大量时间进行字段的设置,行、列、数据区的创建,所以这里就封装了一个针对PivotGridControl的特性类,以及一些公共方法。
开始
特性类
using Agency.Entity.Enum;
using System;
using DevExpress.Data.PivotGrid;
using DevExpress.Utils;
using DevExpress.XtraPivotGrid;
namespace Agency.Entity.PivotGridControl
{
[System.AttributeUsage(AttributeTargets.Property)]
public class PivotGridControlUsageAttribute : Attribute
{
public PivotGridControlUsageAttribute()
{
this.Width = 80;
this.Visible = true;
this.SummaryType = PivotSummaryType.Sum;
}
/// <summary>
/// 列名
/// </summary>
public string Caption { get; set; }
/// <summary>
/// 宽度
/// </summary>
public int Width { get; set; }
/// <summary>
/// 索引
/// </summary>
public int AreaIndex { get; set; }
/// <summary>
/// 视图显示区域
/// </summary>
public PivotArea Area { get; set; }
/// <summary>
/// 统计方式类别
/// </summary>
public PivotSummaryType SummaryType { get; set; }
/// <summary>
/// 换行显示
/// </summary>
public int ColumnValueLineCount { get; set; }
/// <summary>
/// 是否显示
/// </summary>
public bool Visible { get; set; }
//以上为我遇到的一些常用属性,更多的我没有使用到,如果有需要,可继续添加
}
}
作用:定义一个这种特性类的作用,是用于在创建实体类的时候,给实体类中的字段属性加上特性,这样在程序中就不需要一个一个的去选择属性,给属性赋值到指定的特性。
实体类
using Agency.Entity.PivotGridControl;
using DevExpress.Data.PivotGrid;
using DevExpress.XtraPivotGrid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Agency.Entity.Dto
{
public class NewHouseSaleCharRoomDto
{
[PivotGridControlUsage(AreaIndex = 0, Area = PivotArea.RowArea, Visible = false)]
public string RoomID { get; set; }
/// <summary>
/// 房号
/// </summary>
[PivotGridControlUsage(AreaIndex = 0, Area = PivotArea.DataArea, SummaryType = PivotSummaryType.Custom)]
public string RoomNo { get; set; }
/// <summary>
/// 楼层
/// </summary>
[PivotGridControlUsage(Caption = "楼层", AreaIndex = 0, Area = PivotArea.RowArea)]
public Nullable<int> Floor { get; set; }
/// <summary>
/// 单元
/// </summary>
[PivotGridControlUsage(AreaIndex = 0, Area = PivotArea.RowArea, Visible = false)]
public string Cell
{
get
{
return RoomNo.Substring(2, 2);
}
}
/// <summary>
/// 单元名称
/// </summary>
[PivotGridControlUsage(AreaIndex = 0, Area = PivotArea.RowArea, Visible = false)]
public string CellName { get; set; }
/// <summary>
/// 列显示
/// </summary>
[PivotGridControlUsage(AreaIndex = 0, Area = PivotArea.ColumnArea)]
public string ColumnAreaValue
{
get
{
if (CellName == null)
{
return "无单元" + Cell;
}
else
{
return CellName + Cell;
}
}
}
}
}
实体中,给每个字段都添加了特性。
AreaIndex表示列的索引(隐藏的列不会影响),Area表示字段所在的区域,Visible表示该列是否显示。
对于PivotGridControl控件显示数据的结构为:行区域、列区域、和数据区。所以要有三个字段对应三个区域。
在这里我的各个区域对应的字段为:
Floor ----行区域(也就是每一行的行头)
ColumnAreaValue ----列区域(控件会根据列区域的值去动态创建列的个数)
RoomNo ---- 数据区 (就是网格中单元格所在)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Agency.WinForms.Interface;
using DevExpress.XtraBars;
using Agency.WinForms.Common;
using Agency.Entity.Dto;
using DevExpress.XtraTreeList.Nodes;
using Agency.Common;
using Agency.Entity;
using Agency.Common.ExtensionMethods;
using DevExpress.Utils;
using DevExpress.XtraPivotGrid;
using Agency.Common.Utilities;
namespace Agency.WinForms.ControlModule.NewHouse
{
public partial class NewHouseSaleCharControl : BaseListUserControl, IFormToolBar
{
public NewHouseSaleCharControl()
{
InitializeComponent();
}
private void NewHouseSaleCharControl_Load(object sender, EventArgs e)
{
try
{
//禁用总计
pgcNewHouseSaleChar.OptionsView.ShowColumnGrandTotals = false;
pgcNewHouseSaleChar.OptionsView.ShowRowGrandTotals = false;
//设置不显示筛选器标头。
pgcNewHouseSaleChar.OptionsView.ShowFilterHeaders = false;
//设置不允许最终用户拖动字段头。
pgcNewHouseSaleChar.OptionsCustomization.AllowDrag = false;
//设置不显示数据头。
pgcNewHouseSaleChar.OptionsView.ShowDataHeaders = false;
//设置是否显示列标题
pgcNewHouseSaleChar.OptionsView.ShowColumnHeaders = false;
//禁用过滤
pgcNewHouseSaleChar.OptionsCustomization.AllowFilter = false;
//设置内容位置
pgcNewHouseSaleChar.Appearance.FieldValue.TextOptions.HAlignment = HorzAlignment.Center;
pgcNewHouseSaleChar.Appearance.FieldValue.TextOptions.WordWrap = WordWrap.Wrap;
pgcNewHouseSaleChar.Appearance.Cell.TextOptions.HAlignment = HorzAlignment.Center;
}
catch (Exception ex)
{
ShowFormHelper.Error(ex);
}
}
/// <summary>
/// 获取房间数据
/// </summary>
private void GetRoomData()
{
//从后台读取数据
string strID = "1111111000000";
List<NewHouseSaleCharRoomDto> lstNewHouseSaleCharRoomDto = GetApi<List<NewHouseSaleCharRoomDto>>("ID=" + strID ,"NewHouseSaleChar/GetRoomByCondition");
//创建PivotGridField,也就是显示字段
//CreatePovotGridControlColumns该方法为一个封装好的方法
pgcNewHouseSaleChar.CreatePovotGridControlColumns<NewHouseSaleCharRoomDto>();
pgcNewHouseSaleChar.DataSource = lstNewHouseSaleCharRoomDto;
}
}
}
PivotGridControl创建特定列
/// <summary>
/// 给PovotGridControl创建特定的列
/// </summary>
/// <param name="pgv"></param>
/// <param name="listgGridControlColumns"></param>
public static void CreatePovotGridControlColumns(this PivotGridControl pgv, List<PivotGridField> listgGridControlColumns)
{
pgv.DataSource = null;
pgv.Fields.Clear();
if (listgGridControlColumns == null || listgGridControlColumns.Count == 0) return;
foreach (PivotGridField controlColumn in listgGridControlColumns)
{
PivotGridField PivotgridColumn = new PivotGridField()
{
Name = controlColumn.Name,
Caption = controlColumn.Caption,
AreaIndex = controlColumn.AreaIndex,
FieldName = controlColumn.FieldName,
Width = controlColumn.Width,
Area = controlColumn.Area,
SummaryType = controlColumn.SummaryType,
ColumnValueLineCount = controlColumn.ColumnValueLineCount,
Visible = controlColumn.Visible
};
pgv.Fields.Add(PivotgridColumn);
}
}
最终由于数据的不同会出现一个类似的表格