■ DataGridView 클래스의 RowPostPaint 이벤트를 사용해 행 번호를 표시하는 방법을 보여준다.
▶ 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 마우스 DOWN 행 인덱스 /// </summary> private int mouseDownRowIndex; /// <summary> /// 마우스 DOWN 사각형 /// </summary> private Rectangle mouseDownRectangle; /// <summary> /// 드레그 DROP 행 인덱스 /// </summary> private int dragDropRowIndex; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 리스트를 설정한다. List<DataItem> list = new List<DataItem>(); list.Add(new DataItem { Item = "형식" , Description = "쌍발 복좌 고등 훈련기" }); list.Add(new DataItem { Item = "전폭" , Description = "7.70m" }); list.Add(new DataItem { Item = "전장" , Description = "14.14m" }); list.Add(new DataItem { Item = "전고" , Description = "3.92m" }); list.Add(new DataItem { Item = "주익 면적" , Description = "15.8m²" }); list.Add(new DataItem { Item = "최대 이륙 중량", Description = "5,466kg" }); list.Add(new DataItem { Item = "엔진" , Description = "GE J85-GE-5R 터보 제트 엔진 (Dry 995kg, A/B 1,497kg) × 2" }); list.Add(new DataItem { Item = "최대 속도" , Description = "M 1.23/10,973m" }); list.Add(new DataItem { Item = "순항 속도" , Description = "M 0.95/10,973m" }); list.Add(new DataItem { Item = "상승 한도" , Description = "16,335m" }); list.Add(new DataItem { Item = "항속 거리" , Description = "1,768km(최대 연료)" }); list.Add(new DataItem { Item = "이륙활주거리" , Description = "762m" }); list.Add(new DataItem { Item = "착륙활주거리" , Description = "915m" }); list.Add(new DataItem { Item = "승무원" , Description = "2명" }); list.Add(new DataItem { Item = "가격" , Description = "756,000달러(1961년)" }); #endregion #region 데이터 그리드 뷰를 설정한다. this.dataGridView.AllowDrop = true; #endregion SetDataGridViewData(list); #region 이벤트를 설정한다. this.dataGridView.RowPostPaint += dataGridView_RowPostPaint; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 데이터 그리드 뷰 행 포스트 PAINT 처리하기 - dataGridView_RowPostPaint(sender, e) /// <summary> /// 데이터 그리드 뷰 행 포스트 PAINT 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { DataGridView dataGridView = sender as DataGridView; string rowNumber = (e.RowIndex + 1).ToString(); StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; Rectangle drawRectangle = new Rectangle ( e.RowBounds.Left, e.RowBounds.Top, dataGridView.RowHeadersWidth, e.RowBounds.Height ); e.Graphics.DrawString(rowNumber, this.Font, SystemBrushes.ControlText, drawRectangle, stringFormat); } #endregion //////////////////////////////////////////////////////////////////////////////// Functioon #region 데이터 그리드 뷰 셀 스타일 구하기 - GetDataGridViewCellStyle(backgroundColor, alignment) /// <summary> /// 데이터 그리드 뷰 셀 스타일 구하기 /// </summary> /// <param name="backgroundColor">배경색</param> /// <param name="alignment">정렬</param> /// <returns>데이터 그리드 뷰 셀 스타일</returns> private DataGridViewCellStyle GetDataGridViewCellStyle(Color backgroundColor, DataGridViewContentAlignment alignment) { DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Alignment = alignment; style.BackColor = backgroundColor; return style; } #endregion #region 데이터 그리드 뷰 데이터 설정하기 - SetDataGridViewData(dataSource) /// <summary> /// 데이터 그리드 뷰 데이터 설정하기 /// </summary> /// <param name="dataSource">데이터 소스</param> private void SetDataGridViewData(object dataSource) { if(this.dataGridView.DataSource != null) { IDisposable disposable = this.dataGridView.DataSource as IDisposable; this.dataGridView.DataSource = null; if(disposable != null) { disposable.Dispose(); disposable = null; } } #region 리스트 데이터 그리드 뷰 컬럼을 설정한다. this.dataGridView.Columns.Clear(); DataGridViewColumn column; column = new DataGridViewTextBoxColumn(); column.Width = 150; column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; column.HeaderText = "항목"; column.SortMode = DataGridViewColumnSortMode.NotSortable; column.DataPropertyName = "Item"; column.ValueType = typeof(string); column.DefaultCellStyle = GetDataGridViewCellStyle(Color.White, DataGridViewContentAlignment.MiddleLeft); this.dataGridView.Columns.Add(column); column = new DataGridViewTextBoxColumn(); column.Width = 500; column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; column.HeaderText = "설명"; column.SortMode = DataGridViewColumnSortMode.NotSortable; column.DataPropertyName = "Description"; column.ValueType = typeof(string); column.DefaultCellStyle = GetDataGridViewCellStyle(Color.White, DataGridViewContentAlignment.MiddleLeft); this.dataGridView.Columns.Add(column); #endregion this.dataGridView.DataSource = dataSource; } #endregion } } |