■ CardView 클래스에서 상세 뷰 사이의 카드 드래그(DRAG)를 사용하는 방법을 보여준다.
▶ 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 |
using System; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Windows.Forms; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Card; using DevExpress.XtraGrid.Views.Card.ViewInfo; using DevExpress.XtraGrid.Views.Grid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 데이터 셋 /// </summary> private DataSet dataSet = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 데이터를 로드한다. OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\nwind.mdb"); OleDbDataAdapter categoryAdapter = new OleDbDataAdapter ( "SELECT CategoryID, CategoryName, Picture FROM Categories", connection ); OleDbDataAdapter productAdapter = new OleDbDataAdapter ( "SELECT CategoryID, ProductID, ProductName, UnitPrice FROM Products", connection ); this.dataSet = new DataSet(); categoryAdapter.Fill(this.dataSet, "Categories"); productAdapter.Fill (this.dataSet, "Products" ); DataTable categoryTable = this.dataSet.Tables["Categories"]; DataTable productTable = this.dataSet.Tables["Products" ]; productTable.PrimaryKey = new DataColumn[] { productTable.Columns["ProductID"] }; DataColumn keyColumn = categoryTable.Columns["CategoryID"]; DataColumn foreignKeyColumn = productTable.Columns ["CategoryID"]; this.dataSet.Relations.Add("CategoriesProducts", keyColumn, foreignKeyColumn); #endregion #region 저장소 항목 픽처 에디터를 설정한다. RepositoryItemPictureEdit repositoryItemPictureEdit = new RepositoryItemPictureEdit(); repositoryItemPictureEdit.SizeMode = PictureSizeMode.Stretch; #endregion this.gridControl.RepositoryItems.Add(repositoryItemPictureEdit); this.gridControl.AllowDrop = true; CardView cardView = new CardView(this.gridControl); cardView.ViewCaption = "Category Products"; this.gridControl.LevelTree.Nodes.Add("CategoriesProducts", cardView); this.gridControl.DataSource = categoryTable; this.gridView.RowHeight = 50; this.gridView.Columns["CategoryID"].VisibleIndex = -1; this.gridView.Columns["Picture"].ColumnEdit = repositoryItemPictureEdit; this.gridView.Columns["Picture"].OptionsColumn.FixedWidth = true; this.gridView.Columns["Picture"].Width = 180; cardView.PopulateColumns(productTable); cardView.Columns["CategoryID"].VisibleIndex = -1; cardView.Columns["UnitPrice"].DisplayFormat.FormatType = FormatType.Numeric; cardView.Columns["UnitPrice"].DisplayFormat.FormatString = "c2"; this.gridControl.DragOver += gridControl_DragOver; this.gridControl.DragDrop += gridControl_DragDrop; cardView.MouseDown += cardView_MouseDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 그리드 컨트롤 DRAG OVER 처리하기 - gridControl_DragOver(sender, e) /// <summary> /// 그리드 컨트롤 DRAG OVER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_DragOver(object sender, DragEventArgs e) { GridControl gridControl = sender as GridControl; Point point = gridControl.PointToClient(new Point(e.X, e.Y)); BaseView baseView = gridControl.GetViewAt(point); if(baseView is CardView) { e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } } #endregion #region 그리드 컨트롤 DRAG DROP 처리하기 - gridControl_DragDrop(sender, e) /// <summary> /// 그리드 컨트롤 DRAG DROP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_DragDrop(object sender, DragEventArgs e) { GridControl gridControl = sender as GridControl; Point point = gridControl.PointToClient(new Point(e.X, e.Y)); CardView cardView = gridControl.GetViewAt(point) as CardView; string dragData = e.Data.GetData(DataFormats.Text).ToString(); int productID = Convert.ToInt32(dragData); GridView gridView = cardView.ParentView as GridView; object categoryIDObject = gridView.GetRowCellValue ( cardView.SourceRowHandle, gridView.Columns["CategoryID"] ); int categoryID = Convert.ToInt32(categoryIDObject); DataRow dataRow = this.dataSet.Tables["Products"].Rows.Find(productID); if(dataRow != null) { dataRow.SetField("CategoryID", categoryID); } } #endregion #region 카드 뷰 마우스 DOWN 처리하기 - cardView_MouseDown(sender, e) /// <summary> /// 카드 뷰 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cardView_MouseDown(object sender, MouseEventArgs e) { CardView cardView = sender as CardView; GridView gridView = cardView.ParentView as GridView; CardHitInfo cardHitInfo = cardView.CalcHitInfo(e.Location); if(cardHitInfo.HitTest == CardHitTest.CardCaption) { string productID = cardView.GetRowCellValue ( cardHitInfo.RowHandle, cardView.Columns["ProductID"] ).ToString(); this.gridControl.DoDragDrop(productID, DragDropEffects.Move); } } #endregion } } |