■ BandedGridView 클래스에서 컬럼 헤더를 병합하는 방법을 보여준다.
▶ DSGridPainter.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 183 184 185 186 187 188 189 190 191 192 193 194 195 |
using System.Drawing; using System.Linq; using System.Windows.Forms; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Drawing; using DevExpress.XtraGrid.Views.BandedGrid; using DevExpress.XtraGrid.Views.BandedGrid.ViewInfo; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.Drawing; using DevExpress.Utils; using DevExpress.Utils.Drawing; namespace TestProject { /// <summary> /// DS 그리드 페인터 /// </summary> public class DSGridPainter : GridPainter { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 밴드 그리드 뷰 /// </summary> private BandedGridView bandedGridView; /// <summary> /// 그리드 밴드 배열 /// </summary> private GridBand[] gridBandArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DSGridPainter(bandedGridView, gridBandArray) /// <summary> /// 생성자 /// </summary> /// <param name="bandedGridView">밴드 그리드 뷰</param> /// <param name="gridBandArray">그리드 밴드</param> public DSGridPainter(BandedGridView bandedGridView, GridBand[] gridBandArray) : base(bandedGridView) { this.bandedGridView = bandedGridView; this.gridBandArray = gridBandArray; this.bandedGridView.GridControl.Paint += bandedGridView_GridControl_Paint; this.bandedGridView.CustomDrawBandHeader += bandedGridView_CustomDrawBandHeader; this.bandedGridView.CustomDrawColumnHeader += bandedGridView_CustomDrawColumnHeader; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 밴드 그리드 뷰 그리드 컨트롤 페인트시 처리하기 - bandedGridView_GridControl_Paint(sender, e) /// <summary> /// 밴드 그리드 뷰 그리드 컨트롤 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bandedGridView_GridControl_Paint(object sender, PaintEventArgs e) { foreach(GridBand gridBand in this.gridBandArray) { foreach(BandedGridColumn bandedGridColumn in gridBand.Columns) { DrawColumnHeader(new GraphicsCache(e.Graphics), bandedGridColumn); } } } #endregion #region 밴드 그리드 뷰 밴드 헤더 커스텀 그리기 - bandedGridView_CustomDrawBandHeader(sender, e) /// <summary> /// 밴드 그리드 뷰 밴드 헤더 커스텀 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bandedGridView_CustomDrawBandHeader(object sender, BandHeaderCustomDrawEventArgs e) { if(this.gridBandArray.Contains(e.Band)) { e.Handled = true; } } #endregion #region 밴드 그리드 뷰 컬럼 헤더 커스텀 그리기 - bandedGridView_CustomDrawColumnHeader(sender, e) /// <summary> /// 밴드 그리드 뷰 컬럼 헤더 커스텀 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bandedGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) { if(((e.Column is BandedGridColumn)) && (this.gridBandArray.Contains((e.Column as BandedGridColumn).OwnerBand))) { e.Handled = true; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 그리드 밴드 정보 인자 구하기 - GetGridBandInfoArgs(gridBandInfoCollection, gridBand) /// <summary> /// 그리드 밴드 정보 인자 구하기 /// </summary> /// <param name="gridBandInfoCollection">그리드 밴드 정보 컬렉션</param> /// <param name="gridBand">그리드 밴드</param> /// <returns>그리드 밴드 정보 인자</returns> private GridBandInfoArgs GetGridBandInfoArgs(GridBandInfoCollection gridBandInfoCollection, GridBand gridBand) { GridBandInfoArgs returnGridBandInfoArgs = gridBandInfoCollection[gridBand]; if(returnGridBandInfoArgs != null) { return returnGridBandInfoArgs; } else { foreach(GridBandInfoArgs gridBandInfoArgs in gridBandInfoCollection) { if(gridBandInfoArgs.Children != null) { returnGridBandInfoArgs = GetGridBandInfoArgs(gridBandInfoArgs.Children, gridBand); if(returnGridBandInfoArgs != null) { return returnGridBandInfoArgs; } } } } return null; } #endregion #region 컬럼 헤더 그리기 - DrawColumnHeader(graphicsCache, gridColumn) /// <summary> /// 컬럼 헤더 그리기 /// </summary> /// <param name="graphicsCache">그래픽스 캐시</param> /// <param name="gridColumn">그리드 컬럼</param> private void DrawColumnHeader(GraphicsCache graphicsCache, GridColumn gridColumn) { BandedGridViewInfo bandedGridViewInfo = this.bandedGridView.GetViewInfo() as BandedGridViewInfo; GridColumnInfoArgs gridColumnInfoArgs = bandedGridViewInfo.ColumnsInfo[gridColumn]; GridBandInfoArgs gridBandInfoArgs = GetGridBandInfoArgs(bandedGridViewInfo.BandsInfo, (gridColumn as BandedGridColumn).OwnerBand); if(gridColumnInfoArgs == null || gridBandInfoArgs == null) { return; } gridColumnInfoArgs.Cache = graphicsCache; int top = gridBandInfoArgs.Bounds.Top; Rectangle rectangle = gridColumnInfoArgs.Bounds; int delta = rectangle.Top - top; rectangle.Y = top; rectangle.Height += delta; gridColumnInfoArgs.Bounds = rectangle; gridColumnInfoArgs.Appearance.TextOptions.VAlignment = VertAlignment.Center; ElementsPainter.Column.CalcObjectBounds(gridColumnInfoArgs); ElementsPainter.Column.DrawObject(gridColumnInfoArgs); } #endregion } } |
▶ 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 |
using System; using System.Data; using System.Drawing; using System.Windows.Forms; using DevExpress.XtraGrid.Views.BandedGrid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.gridView.Appearance.BandPanel.Font = new Font("나눔고딕코딩", 12f); this.gridView.Appearance.HeaderPanel.Font = new Font("나눔고딕코딩", 12f); this.gridView.Appearance.Row.Font = new Font("나눔고딕코딩", 12f); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.gridControl.DataSource = new DataTable(); DSGridPainter dsGridPainter = new DSGridPainter(this.gridView, new GridBand[] { this.gridBand1 }); this.gridControl.DataSource = null; this.gridControl.DataSource = GetSampleTable(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 샘플 테이블 구하기 - GetSampleTable() /// <summary> /// 샘플 테이블 구하기 /// </summary> /// <returns>샘플 테이블</returns> private DataTable GetSampleTable() { DataTable table = new DataTable(); table.Columns.Add("bandedGridColumn1", typeof(string)); table.Columns.Add("bandedGridColumn2", typeof(string)); table.Columns.Add("bandedGridColumn3", typeof(string)); table.Columns.Add("bandedGridColumn4", typeof(string)); table.Columns.Add("bandedGridColumn5", typeof(string)); table.Rows.Add(new object[] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty }); return table; } #endregion } } |