xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
页面:
RowTreeWidth="130" ShowFilterHeaders="False" ChartSelectionOnly="False" ChartProvideDataByColumns ="True" ShowRowHeaders="False" ShowColumnHeaders="False" ShowDataHeaders="False" ChartShowColumnGrandTotals="False" ChartShowRowGrandTotals="False"> Caption="行字段标题" Width="155" /> CellFormat="c" /> Area="ColumnArea" AreaIndex="0" GroupInterval="DateMonth" Caption="列字段标题" />
数据绑定;
public void SetDataSource(DataTable dt, string f_Value, string f_Count, string f_Col, string f_Row)
{
this.pivotGrid.DataSource = null;
this.pivotGrid.Fields.Clear();
this.pivotGrid.DataSource = dt;//数据源
this.pivotGrid.Fields.Add(f_Row, DevExpress.Xpf.PivotGrid.FieldArea.RowArea);//横向列名
this.pivotGrid.Fields.Add(f_Col, DevExpress.Xpf.PivotGrid.FieldArea.ColumnArea);//纵向列名
F_Value = f_Value;// 根据 F_Value 分类统计
F_Count = f_Count;// 根据 F_Count 分类统计(用于多个分类统计时,当数据源不变时)
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields.Add(f_Value, DevExpress.Xpf.PivotGrid.FieldArea.DataArea);
}
else
{
this.pivotGrid.Fields.Add(f_Col, DevExpress.Xpf.PivotGrid.FieldArea.DataArea);
}
}
//修改 列名
private void PivotGridControl2_FieldValueDisplayText(object sender, DevExpress.Xpf.PivotGrid.PivotFieldDisplayTextEventArgs e)
{
if(e.ValueType==DevExpress.Xpf.PivotGrid.FieldValueType.GrandTotal)//总计
{
if (e.IsColumn && e.DisplayText.Trim() == "Grand Total")//第一层列总计标题
{
e.DisplayText = "总计";
}
else if (e.IsColumn)//其他层列总计标题
{
e.DisplayText = e.DisplayText + "总计";
}
else if (e.IsColumn == false && e.DisplayText.Trim() == "Grand Total")//第一层行总计标题
{
e.DisplayText = "总计";
}
else
{
e.DisplayText = "";
}
}
}
private void radButArea_Checked(object sender, RoutedEventArgs e)
{
if (this.pivotGrid == null) return;
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields[2].FieldName = F_Value;
}
else
{
this.pivotGrid.Fields[2].FieldName = F_Count;
}
}
private void radButNumber_Checked(object sender, RoutedEventArgs e)
{
if (this.pivotGrid == null) return;
if (this.radButArea.IsChecked == true)
{
this.pivotGrid.Fields[2].FieldName = F_Value;
}
else
{
this.pivotGrid.Fields[2].FieldName = F_Count;
}
}
//导出
private void KImgButton_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".xlsx"; // Default file extension
dlg.Filter = "excel documents (.xlsx)|*.xlsx"; // Filter files by extension
// Show save file dialog box
Nullable
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
this.pivotGrid.ExportToXlsx(filename);
}
}