■ GridControl 클래스의 DoDragDrop 메소드를 사용해 드래그 & 드롭하는 방법을 보여준다.
▶ 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
using System; using System.Data; using System.Drawing; using System.Windows.Forms; using DevExpress.Data; using DevExpress.Utils; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; namespace TestProject { /// <summary> /// 테스트 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.sourceGridView.Appearance.HeaderPanel.Font = new Font("나눔고딕코딩", 12f); this.sourceGridView.Appearance.Row.Font = new Font("나눔고딕코딩", 12f); this.targetGridView.Appearance.HeaderPanel.Font = new Font("나눔고딕코딩", 12f); this.targetGridView.Appearance.Row.Font = new Font("나눔고딕코딩", 12f); Load += Form_Load; this.sourceGridView.MouseDown += sourceGridView_MouseDown; this.targetGridControl.DragDrop += targetGridControl_DragDrop; this.targetGridControl.DragOver += targetGridControl_DragOver; } #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) { SetGridView(this.sourceGridView, "sourceGridView", false, false, false, GridMultiSelectMode.CellSelect, false, false); this.sourceGridView.Columns.Add ( GetGridColumn ( "ID", "ID", UnboundColumnType.Integer, 100, HorzAlignment.Far, false, FormatType.None, null ) ); this.sourceGridView.Columns.Add ( GetGridColumn ( "명칭", "Name", UnboundColumnType.Integer, 100, HorzAlignment.Near, false, FormatType.None, null ) ); SetGridView(this.targetGridView, "targetGridView", false, false, false, GridMultiSelectMode.CellSelect, false, false); this.targetGridView.Columns.Add ( GetGridColumn ( "ID", "ID", UnboundColumnType.Integer, 100, HorzAlignment.Far, false, FormatType.None, null ) ); this.targetGridView.Columns.Add ( GetGridColumn ( "명칭", "Name", UnboundColumnType.Integer, 100, HorzAlignment.Near, false, FormatType.None, null ) ); DataTable sourceTable = GetEmptyList(); SetList(sourceTable); this.sourceGridControl.DataSource = sourceTable; DataTable targetTable = GetEmptyList(); this.targetGridControl.DataSource = targetTable; } #endregion #region 소스 마우스 버튼 DOWN 처리하기 - sourceGridView_MouseDown(sender, e) /// <summary> /// 소스 마우스 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sourceGridView_MouseDown(object sender, MouseEventArgs e) { GridHitInfo gridHitInfo = this.sourceGridView.CalcHitInfo(e.Location); if(gridHitInfo.InRowCell) { this.sourceGridView.FocusedRowHandle = gridHitInfo.RowHandle; DataRow row = this.sourceGridView.GetDataRow(gridHitInfo.RowHandle); this.sourceGridControl.DoDragDrop(row, DragDropEffects.Move); } } #endregion #region 타겟 목록 드래그 오버시 처리하기 - targetGridControl_DragOver(sender, e) /// <summary> /// 타겟 목록 드래그 오버시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetGridControl_DragOver(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(typeof(DataRow))) { if((e.AllowedEffect & DragDropEffects.Move) != 0) { e.Effect = DragDropEffects.Move; } } else { e.Effect = DragDropEffects.None; } } #endregion #region 타겟 목록 드래그 드롭시 처리하기 - targetGridControl_DragDrop(sender, e) /// <summary> /// 타겟 목록 드래그 드롭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetGridControl_DragDrop(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(typeof(DataRow))) { DataRow sourceRow = e.Data.GetData(typeof(DataRow)) as DataRow; DataTable targetTable = this.targetGridControl.DataSource as DataTable; DataRow targetRow = targetTable.NewRow(); for(int i = 0; i < targetTable.Columns.Count; i++) { targetRow[i] = sourceRow[i]; } targetTable.Rows.Add(targetRow); DataTable sourceTable = this.sourceGridControl.DataSource as DataTable; sourceTable.Rows.Remove(sourceRow); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 그리드 뷰 설정하기 - SetGridView(gridView, name, sortable, editable, multiSelect, gridMultiSelectMode, showGroupPanel, showIndicator) /// <summary> /// 그리드 뷰 설정하기 /// </summary> /// <param name="gridView">GridView</param> /// <param name="name">명칭</param> /// <param name="sortable">정렬 가능 여부</param> /// <param name="editable">편집 가능 여부</param> /// <param name="multiSelect">복수 선택 여부</param> /// <param name="gridMultiSelectMode">그리드 복수 선택 모드</param> /// <param name="showGroupPanel">그룹 패널 표시 여부</param> /// <param name="showIndicator">지시자 표시 여부</param> /// <returns>그리드 뷰</returns> private void SetGridView ( GridView gridView, string name, bool sortable, bool editable, bool multiSelect, GridMultiSelectMode gridMultiSelectMode, bool showGroupPanel, bool showIndicator ) { gridView.Name = name; gridView.OptionsBehavior.AutoPopulateColumns = false; gridView.OptionsBehavior.Editable = editable; gridView.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown; gridView.OptionsCustomization.AllowColumnMoving = false; gridView.OptionsCustomization.AllowColumnResizing = true; gridView.OptionsCustomization.AllowFilter = false; gridView.OptionsCustomization.AllowSort = sortable; gridView.OptionsMenu.EnableColumnMenu = false; gridView.OptionsSelection.MultiSelect = multiSelect; gridView.OptionsSelection.MultiSelectMode = gridMultiSelectMode; gridView.OptionsView.ColumnAutoWidth = false; gridView.OptionsView.EnableAppearanceEvenRow = true; gridView.OptionsView.ShowGroupPanel = showGroupPanel; gridView.OptionsView.ShowIndicator = showIndicator; } #endregion #region 그리드 컬럼 구하기 - GetGridColumn(caption, fieldName, unboundColumnType, width, horzAlignment, allowEdit, formatType, formatString) /// <summary> /// 그리드 컬럼 구하기 /// </summary> /// <param name="caption">제목</param> /// <param name="fieldName">필드명</param> /// <param name="unboundColumnType">언바운드 컬럼 종류</param> /// <param name="width">너비</param> /// <param name="horzAlignment">수평 정렬</param> /// <param name="allowEdit">편집 허용 여부</param> /// <param name="formatType">형식 종류</param> /// <param name="formatString">형식 문자열</param> /// <returns>그리드 컬럼</returns> private GridColumn GetGridColumn ( string caption, string fieldName, UnboundColumnType unboundColumnType, int width, HorzAlignment horzAlignment, bool allowEdit, FormatType formatType, string formatString ) { GridColumn gridColumn = new GridColumn(); gridColumn.Name = string.Empty; gridColumn.Caption = caption; gridColumn.FieldName = fieldName; gridColumn.UnboundType = unboundColumnType; gridColumn.Width = width; gridColumn.AppearanceHeader.Options.UseTextOptions = true; gridColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center; gridColumn.AppearanceHeader.TextOptions.VAlignment = VertAlignment.Center; gridColumn.AppearanceCell.Options.UseTextOptions = true; gridColumn.AppearanceCell.TextOptions.HAlignment = horzAlignment; gridColumn.AppearanceCell.TextOptions.VAlignment = VertAlignment.Center; gridColumn.OptionsColumn.AllowEdit = allowEdit; gridColumn.DisplayFormat.FormatType = formatType; gridColumn.DisplayFormat.FormatString = formatString; gridColumn.Visible = true; return gridColumn; } #endregion #region 비어있는 목록 구하기 - GetEmptyList() /// <summary> /// 비어있는 목록 구하기 /// </summary> /// <returns>비어있는 목록</returns> private DataTable GetEmptyList() { DataTable table = new DataTable(); table.Columns.Add(new DataColumn("ID" , typeof(int ))); table.Columns.Add(new DataColumn("Name", typeof(string))); return table; } #endregion #region 목록 설정하기 - SetList(sourceTable) /// <summary> /// 목록 설정하기 /// </summary> /// <param name="sourceTable">소스 테이블</param> private void SetList(DataTable sourceTable) { DataRow row = null; for(int i = 1; i < 101; i++) { row = sourceTable.NewRow(); row["ID" ] = i; row["Name"] = "목록 " + i.ToString(); sourceTable.Rows.Add(row); } } #endregion } } |