■ GridControl 클래스의 DoDragDrop 메소드를 사용해 내부에서 마우스 드래그 & 드롭을 처리하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System.Data; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; ... private GridControl gridControl; private GridView gridView; ... this.gridControl.MouseDown += new gridControl_MouseDown; this.gridControl.MouseMove += new gridControl_MouseMove; this.gridControl.DragOver += new gridControl_DragOver; this.gridControl.DragDrop += new gridControl_DragDrop; ... private GridHitInfo gridHitInfo = null; ... #region 그리드 컨트롤 마우스 DOWN 처리하기 - gridControl_MouseDown(sender, e) /// <summary> /// 그리드 컨트롤 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_MouseDown(object sender, MouseEventArgs e) { this.gridHitInfo = this.gridView.CalcHitInfo(new Point(e.X, e.Y)); if(this.gridHitInfo.RowHandle < 0) { this.gridHitInfo = null; } } #endregion #region 그리드 컨트롤 마우스 이동시 처리하기 - gridControl_MouseMove(sender, e) /// <summary> /// 그리드 컨트롤 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_MouseMove(object sender, MouseEventArgs e) { if(this.gridHitInfo == null) { return; } if(e.Button != MouseButtons.Left) { return; } Rectangle dragRectangle = new Rectangle ( new Point ( this.gridHitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2, this.gridHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2 ), SystemInformation.DragSize ); if(!dragRectangle.Contains(new Point(e.X, e.Y))) { if(this.gridHitInfo.InRow) { this.gridControl.DoDragDrop(new object[] { this.gridHitInfo.RowHandle, gridControl }, DragDropEffects.All); } } } #endregion #region 그리드 컨트롤 드래그 OVER 처리하기 - gridControl_DragOver(sender, e) /// <summary> /// 그리드 컨트롤 드래그 OVER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_DragOver(object sender, DragEventArgs e) { GridHitInfo gridHitInfo = this.gridView.CalcHitInfo(this.gridControl.PointToClient(new Point(e.X, e.Y))); int sourceRowHandle = GetDragObject(sender, e.Data); if(gridHitInfo.InRow && sourceRowHandle >= 0 && sourceRowHandle != gridHitInfo.RowHandle) { if((e.KeyState & 8) != 0) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.Move; } } else { e.Effect = DragDropEffects.None; } } #endregion #region 그리드 컨트롤 드래그 DROP 처리하기 - gridControl_DragDrop(sender, e) /// <summary> /// 그리드 컨트롤 드래그 DROP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_DragDrop(object sender, DragEventArgs e) { int sourceRowHandle = GetDragObject(sender, e.Data); if(sourceRowHandle < 0) { return; } GridHitInfo targetGridHitInfo = this.gridView.CalcHitInfo ( this.gridControl.PointToClient(new Point(e.X, e.Y)) ); if(targetGridHitInfo.InRow) { DataRowView sourceDataRowView = this.gridView.GetRow(sourceRowHandle) as DataRowView; int targetRowHandle = targetGridHitInfo.RowHandle; if(targetRowHandle < 0) { this.gridView.SetRowExpanded(targetRowHandle, true, true); targetRowHandle = this.gridView.GetDataRowHandleByGroupRowHandle(targetRowHandle); } DataRow targetDataRow = this.gridView.GetDataRow(targetRowHandle); DataRowView newDataRowView = null; if(e.Effect == DragDropEffects.Copy) { newDataRowView = ((DataView)this.gridView.DataSource).AddNew(); for(int i = 0; i < this.gridView.Columns.Count; i++) { newDataRowView.Row[this.gridView.Columns[i].FieldName] = sourceDataRowView[this.gridView.Columns[i].FieldName]; } sourceDataRowView = newDataRowView; } for(int i = 0; i < this.gridView.SortInfo.GroupCount; i++) { sourceDataRowView.Row[this.gridView.GroupedColumns[i].FieldName] = targetDataRow[this.gridView.GroupedColumns[i].FieldName]; } if(newDataRowView != null) { newDataRowView.Row["id"] = ((DataView)this.gridView.DataSource).Count; } sourceDataRowView.EndEdit(); } } #endregion #region 드래그 객체 구하기 - GetDragObject(sender, dataObject) /// <summary> /// 드래그 객체 구하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="dataObject">데이터 객체</param> /// <returns>드래그 객체</returns> private int GetDragObject(object sender, IDataObject dataObject) { object[] objectArray = dataObject.GetData(typeof(object[])) as object[]; if(objectArray == null || !sender.Equals(this.gridControl)) { return -2; } if(objectArray[0] is int) { return (int)objectArray[0]; } else { return -1; } } #endregion |