■ DataGridView 클래스의 컬럼에서 동적 콤보 박스를 사용하는 방법을 보여준다.
▶ ListItem.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 |
namespace TestProject { /// <summary> /// 리스트 항목 /// </summary> public class ListItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 값 - Value /// <summary> /// 값 /// </summary> public string Value { get; set; } #endregion #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get; set; } #endregion #region 태그 - Tag /// <summary> /// 태그 /// </summary> public object Tag { get; set; } #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 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
using System; using System.Collections.ObjectModel; using System.Text; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Private #region 그리드 항목 - GridItem /// <summary> /// 그리드 항목 /// </summary> private class GridItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get; set; } #endregion #region 타입 - Type /// <summary> /// 타입 /// </summary> public string Type { get; set; } #endregion #region 값 1 - Value1 /// <summary> /// 값 1 /// </summary> public string Value1 { get; set; } #endregion #region 값 2 - Value2 /// <summary> /// 값 2 /// </summary> public string Value2 { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 컬렉션 구하기 - GetCollection() /// <summary> /// 컬렉션 구하기 /// </summary> /// <returns>컬렉션</returns> public static ObservableCollection<GridItem> GetCollection() { ObservableCollection<GridItem> collection = new ObservableCollection<GridItem>(); collection.Add(new GridItem() { Title = "항목1", Type = "A" , Value1 = "A", Value2 = "" }); collection.Add(new GridItem() { Title = "항목2", Type = "B" , Value1 = "" , Value2 = "B" }); collection.Add(new GridItem() { Title = "항목3", Type = "A" , Value1 = "A", Value2 = "" }); collection.Add(new GridItem() { Title = "항목4", Type = "Both", Value1 = "A", Value2 = "B" }); collection.Add(new GridItem() { Title = "항목5", Type = "B" , Value1 = "" , Value2 = "B" }); return collection; } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); InitializeColumns(); this.dataGridView.AllowUserToAddRows = false; this.dataGridView.AllowUserToDeleteRows = false; this.dataGridView.AllowUserToOrderColumns = false; this.dataGridView.AllowUserToResizeRows = false; this.dataGridView.RowHeadersVisible = false; this.dataGridView.DataSource = GridItem.GetCollection(); this.dataGridView.CellClick += dataGridView_CellClick; this.dataGridView.EditingControlShowing += dataGridView_EditingControlShowing; this.dataGridView.CellValueChanged += dataGridView_CellValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 데이터 그리드 뷰 셀 클릭시 처리하기 - dataGridView_CellClick(sender, e) /// <summary> /// 데이터 그리드 뷰 셀 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if((e.RowIndex < 0) || (e.ColumnIndex < 0)) { return; } if(e.ColumnIndex != 1) { return; } DataGridViewRow dataGridViewRow = this.dataGridView.Rows[e.RowIndex]; DataGridViewCell dataGridViewCell = dataGridViewRow.Cells[e.ColumnIndex]; if(dataGridViewCell is DataGridViewComboBoxCell) { dataGridView.CurrentCell = dataGridViewCell; dataGridView.BeginEdit(true); DataGridViewComboBoxEditingControl comboboxEdit = (DataGridViewComboBoxEditingControl)this.dataGridView.EditingControl; if(comboboxEdit != null) { comboboxEdit.DroppedDown = true; } } } #endregion #region 데이터 그리드 뷰 편집 컨트롤 표시시 처리하기 - dataGridView_EditingControlShowing(sender, e) /// <summary> /// 데이터 그리드 뷰 편집 컨트롤 표시시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { int rowIndex = this.dataGridView.CurrentCell.RowIndex; string y1Name = this.dataGridView.Rows[rowIndex].Cells[2].Value as string; string y2Name = this.dataGridView.Rows[rowIndex].Cells[3].Value as string; ComboBox comboBox = e.Control as ComboBox; if(comboBox != null) { object value = comboBox.SelectedItem; comboBox.SelectedIndexChanged -= comboBox_SelectedIndexChanged; comboBox.Items.Clear(); if(y1Name.Length > 0 && y2Name.Length > 0) { comboBox.Items.AddRange(new object[] { "Both", "A", "B" }); } else if(y1Name.Length > 0 && y2Name.Length == 0) { comboBox.Items.AddRange(new object[] { "A", "B" }); } else if(y1Name.Length == 0 && y2Name.Length > 0) { comboBox.Items.AddRange(new object[] { "A", "B" }); } else { comboBox.Items.AddRange(new object[] { "Both", "A", "B" }); } comboBox.SelectedItem = value; comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged; } } #endregion #region 콤보 박스 선택 인덱스 변경시 처리하기 - comboBox_SelectedIndexChanged(sender, e) /// <summary> /// 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { try { this.dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); this.dataGridView.UpdateCellValue(dataGridView.CurrentCell.ColumnIndex, dataGridView.CurrentCell.RowIndex); } catch(Exception exception) { MessageBox.Show(exception.ToString()); } } #endregion #region 데이터 그리드 뷰 셀 값 변경시 처리하기 - dataGridView_CellValueChanged(sender, e) /// <summary> /// 데이터 그리드 뷰 셀 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("데이터 그리드 뷰 결과"); stringBuilder.AppendLine(); foreach(DataGridViewRow row in dataGridView.Rows) { stringBuilder.Append("행" + row.Index + "\t"); foreach(DataGridViewCell cell in row.Cells) { if(cell.Value == null) { stringBuilder.Append("null\t"); } else { stringBuilder.Append(cell.Value.ToString() + "\t"); } } stringBuilder.AppendLine(); } MessageBox.Show(stringBuilder.ToString()); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 컬럼들 초기화 하기 - InitializeColumns() /// <summary> /// 컬럼들 초기화 하기 /// </summary> private void InitializeColumns() { this.dataGridView.Columns.Clear(); DataGridViewTextBoxColumn textBoxColumn; textBoxColumn = new DataGridViewTextBoxColumn(); textBoxColumn.Name = "Title"; textBoxColumn.HeaderText = "제목"; textBoxColumn.DataPropertyName = "Title"; textBoxColumn.ReadOnly = true; textBoxColumn.Visible = true; this.dataGridView.Columns.Add(textBoxColumn); DataGridViewComboBoxColumn comboBoxColumn; comboBoxColumn = new DataGridViewComboBoxColumn(); comboBoxColumn.Name = "Type"; comboBoxColumn.HeaderText = "타입"; comboBoxColumn.DataPropertyName = "Type"; comboBoxColumn.ReadOnly = false; comboBoxColumn.Visible = true; comboBoxColumn.Items.AddRange("Both", "A", "B"); this.dataGridView.Columns.Add(comboBoxColumn); textBoxColumn = new DataGridViewTextBoxColumn(); textBoxColumn.Name = "Value1"; textBoxColumn.HeaderText = "Value1"; textBoxColumn.DataPropertyName = "Value1"; textBoxColumn.ReadOnly = true; textBoxColumn.Visible = false; this.dataGridView.Columns.Add(textBoxColumn); textBoxColumn = new DataGridViewTextBoxColumn(); textBoxColumn.Name = "Value2"; textBoxColumn.HeaderText = "Value2"; textBoxColumn.DataPropertyName = "Value2"; textBoxColumn.ReadOnly = true; textBoxColumn.Visible = false; this.dataGridView.Columns.Add(textBoxColumn); } #endregion } } |