■ PivotGridControl 클래스의 SavePivotGridToFile 메소드를 사용해 지역 데이터 저장소를 생성하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
using System; using System.Windows.Forms; using DevExpress.Data; using DevExpress.Data.PivotGrid; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraPivotGrid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 피벗 그리드 컨트롤을 설정한다. this.pivotGridControl.Margin = new Padding(0); this.pivotGridControl.Appearance.FieldValue.TextOptions.HAlignment = HorzAlignment.Center; PivotGridField category1Field = new PivotGridField(); category1Field.Name = string.Empty; category1Field.Caption = "대분류"; category1Field.FieldName = "CATEGORY1"; category1Field.Area = PivotArea.ColumnArea; category1Field.AreaIndex = 0; category1Field.Appearance.Header.Options.UseTextOptions = true; category1Field.Appearance.Header.TextOptions.HAlignment = HorzAlignment.Center; category1Field.Appearance.Cell.Options.UseTextOptions = true; category1Field.Appearance.Cell.TextOptions.HAlignment = HorzAlignment.Default; category1Field.CellFormat.FormatType = FormatType.None; category1Field.CellFormat.FormatString = null; category1Field.UnboundType = UnboundColumnType.Bound; PivotGridField category2Field = new PivotGridField(); category2Field.Name = string.Empty; category2Field.Caption = "중분류"; category2Field.FieldName = "CATEGORY2"; category2Field.Area = PivotArea.ColumnArea; category2Field.AreaIndex = 1; category2Field.Appearance.Header.Options.UseTextOptions = true; category2Field.Appearance.Header.TextOptions.HAlignment = HorzAlignment.Center; category2Field.Appearance.Cell.Options.UseTextOptions = true; category2Field.Appearance.Cell.TextOptions.HAlignment = HorzAlignment.Default; category2Field.CellFormat.FormatType = FormatType.None; category2Field.CellFormat.FormatString = null; category2Field.UnboundType = UnboundColumnType.Bound; PivotGridField yyyyMMField = new PivotGridField(); yyyyMMField.Name = string.Empty; yyyyMMField.Caption = "연월"; yyyyMMField.FieldName = "YYYYMM"; yyyyMMField.Area = PivotArea.RowArea; yyyyMMField.AreaIndex = 0; yyyyMMField.Appearance.Header.Options.UseTextOptions = true; yyyyMMField.Appearance.Header.TextOptions.HAlignment = HorzAlignment.Center; yyyyMMField.Appearance.Cell.Options.UseTextOptions = true; yyyyMMField.Appearance.Cell.TextOptions.HAlignment = HorzAlignment.Center; yyyyMMField.CellFormat.FormatType = FormatType.None; yyyyMMField.CellFormat.FormatString = null; yyyyMMField.UnboundType = UnboundColumnType.Bound; PivotGridField amountField = new PivotGridField(); amountField.Name = string.Empty; amountField.Caption = "금액"; amountField.FieldName = "AMOUNT"; amountField.Area = PivotArea.DataArea; amountField.AreaIndex = 0; amountField.Appearance.Header.Options.UseTextOptions = true; amountField.Appearance.Header.TextOptions.HAlignment = HorzAlignment.Center; amountField.Appearance.Cell.Options.UseTextOptions = true; amountField.Appearance.Cell.TextOptions.HAlignment = HorzAlignment.Far; amountField.CellFormat.FormatType = FormatType.Numeric; amountField.CellFormat.FormatString = "#,##0"; amountField.UnboundType = UnboundColumnType.Bound; this.pivotGridControl.Fields.Add(category1Field); this.pivotGridControl.Fields.Add(category2Field); this.pivotGridControl.Fields.Add(yyyyMMField ); this.pivotGridControl.Fields.Add(amountField ); #endregion #region 이벤트를 설정한다. this.pivotGridControl.FieldValueDisplayText += pivotGridControl_FieldValueDisplayText; this.saveButton.Click += saveButton_Click; this.restoreButton.Click += restoreButton_Click; #endregion this.pivotGridControl.DataSource = Item.GetItemList(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 피벗 그리드 컨트롤 필드 값 표시 텍스트 처리하기 - pivotGridControl_FieldValueDisplayText(sender, e) /// <summary> /// 피벗 그리드 컨트롤 필드 값 표시 텍스트 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pivotGridControl_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) { if(e.ValueType == PivotGridValueType.GrandTotal) { if(e.IsColumn) { e.DisplayText = "소계"; } else { e.DisplayText = "총계"; } } else if(e.ValueType == PivotGridValueType.Total) { e.DisplayText = $"{e.Value} 소계"; } } #endregion #region 저장 버튼 클릭시 처리하기 - saveButton_Click(sender, e) /// <summary> /// 저장 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveButton_Click(object sender, EventArgs e) { string filePath = "pivot.dat"; this.pivotGridControl.SavePivotGridToFile(filePath); this.pivotGridControl.DataSource = null; } #endregion #region 복원 버튼 클릭시 처리하기 - restoreButton_Click(sender, e) /// <summary> /// 복원 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void restoreButton_Click(object sender, EventArgs e) { string filePath = "pivot.dat"; PivotFileDataSource dataSource = new PivotFileDataSource(filePath); this.pivotGridControl.DataSource = dataSource; } #endregion } } |