通过使用点击信息,可以完全控制网格元素的行为。 点击信息概述 章节描述了如何获取点击信息。 本主题提供了许多示例,这些示例演示如何为网格元素实现自定义行为。
实现自定义弹出式菜单
下面的示例演示了如何实现网格行的弹出式菜单。 此菜单只有一个菜单项,此菜单项用于删除调用菜单的行。 调用 GridView.CalcHitInfo 方法来获取行句柄。 然后把焦点移到此行,并调用自定义上下文菜单。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Menu; using DevExpress.Utils.Menu; private void gridView1_MouseDown(object sender, MouseEventArgs e) { GridView view = sender as GridView; if(view == null) return; // obtaining hit info GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y)); if (((e.Button & MouseButtons.Right) != 0) && (hitInfo.InRow) && (!view.IsGroupRow(hitInfo.RowHandle))) { // switching focus view.FocusedRowHandle = hitInfo.RowHandle; // showing the custom context menu ViewMenu menu = new ViewMenu(view); DXMenuItem menuItem = new DXMenuItem("DeleteRow", new EventHandler(DeleteFocusedRow)); menuItem.Tag = view; menu.Items.Add(menuItem); menu.Show(hitInfo.HitPoint); } } void DeleteFocusedRow(object sender, EventArgs e) { DXMenuItem menuItem = sender as DXMenuItem; if(menuItem == null) return; ColumnView View = menuItem.Tag as ColumnView; View.DeleteRow(view.FocusedRowHandle); } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Grid; Imports DevExpress.XtraGrid.Views.Grid.ViewInfo; Imports DevExpress.XtraGrid.Views.Base; Imports DevExpress.XtraGrid.Menu; Imports DevExpress.Utils.Menu; Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _ Handles gridView1.MouseDown Dim View As GridView = CType(sender, GridView) If View Is Nothing Then Return ' obtaining hit info Dim hitInfo As GridHitInfo = View.CalcHitInfo(New Point(e.X, e.Y)) If ((e.Button & MouseButtons.Right) <> 0) And (hitInfo.InRow) And _ (Not View.IsGroupRow(hitInfo.RowHandle)) Then ' switching focus View.FocusedRowHandle = hitInfo.RowHandle ' showing the custom context menu Dim menu As ViewMenu = New ViewMenu(view) Dim menuItem As DXMenuItem = New DXMenuItem("DeleteRow", _ New EventHandler(AddressOf DeleteFocusedRow)) menuItem.Tag = view menu.Items.Add(menuItem) menu.Show(hitInfo.HitPoint) End If End Sub Private Sub DeleteFocusedRow(ByVal sender As Object, ByVal e As EventArgs) Dim menuItem As DXMenuItem = CType(sender, DXMenuItem) If menuItem Is Nothing Then Return Dim View As ColumnView = CType(menuItem.Tag, ColumnView) View.DeleteRow(view.FocusedRowHandle) End Sub |
下面的屏幕截图展示了上述代码的执行结果:
在视图内实现自定义行为
下面的示例假设在网格控件中有一个主/从关系。 主表视图由 GridView 呈现,细节克隆视图由 CardView 类的实例呈现。 下面的示例展示了如何在细节克隆视图之间实现拖放操作。
System.Windows.Forms.MouseDown 事件用于识别被单击的卡片。 为了达到此目的,调用了 CardView.CalcHitInfo 方法。 然后 CardHitInfo.HitTest 属性用于确定被单击的点是否属于卡片标题区域,如果属于则通过调用 DoDragDrop 方法开始拖动。 System.Windows.Forms.DragOver 事件用于确定被拖动卡片的位置。 GridControl.GetViewAt 方法确定当前被拖动卡片经过的视图。 如果返回的视图不是卡片视图,则禁止放落操作。
最后,接管了 System.Windows.Forms.DragDrop 事件,把被拖动卡片从源克隆视图移动到目标克隆视图。 GridControl.GetViewAt 方法用于获取卡片被放落在哪个视图中。 通过修改引用主表的列中的单元格取值,实际完成卡片移动。
注意,要允许在网格控件中放落,需要把 AllowDrop 属性值设置为 true。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Card; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Card.ViewInfo; // ... private void cvProducts_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { CardView View = sender as CardView; GridView parentView = View.ParentView as GridView; // obtaining the clicked point CardHitInfo hi = View.CalcHitInfo(new Point(e.X, e.Y)); // determining if the clicked point belongs to a card caption if (hi.HitTest == CardHitTest.CardCaption) { // obtaining the clicked card string rowID = View.GetRowCellValue(hi.RowHandle, View.Columns["ProductID"]).ToString(); gridControl1.DoDragDrop(rowID, DragDropEffects.Move); } } // ... private void gridControl1_DragOver(object sender, DragEventArgs e) { GridControl grid = sender as GridControl; // obtaining the View over which the card is being dragged BaseView View = grid.GetViewAt(new Point(e.X, e.Y)); if (view is CardView) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } // ... private void gridControl1_DragDrop(object sender, DragEventArgs e) { GridControl grid = sender as GridControl; // obtaining the View on which the card has been dropped Point pt = gridControl1.PointToClient(new Point(e.X, e.Y)); CardView View = grid.GetViewAt(pt) as CardView; string dragData = e.Data.GetData(DataFormats.Text).ToString(); int rowID = Convert.ToInt32(dragData); // obtaining the row which owns the detail clone that the card has been dropped on GridView parentView = View.ParentView as GridView; object cellValue = parentView.GetRowCellValue(view.SourceRowHandle, parentView.Columns["CategoryID"]); int currentParentID = Convert.ToInt32(cellValue); // changing the parent row of the card results in moving this card to the specified parent row: dataSet11.Products.Rows.Find(rowID)[dataSet11.Products.Columns["CategoryID"]] = currentParentID; } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Base Imports DevExpress.XtraGrid.Views.Card Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid Imports DevExpress.XtraGrid.Views.Card.ViewInfo // ... Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _ sHandles MyBase.MouseDown Dim View As CardView = sender Dim ParentView As GridView = View.ParentView ' obtaining the clicked point Dim Hi As CardHitInfo = View.CalcHitInfo(New Point(e.X, e.Y)) ' determining if the clicked point belongs to a card caption If Hi.HitTest = CardHitTest.CardCaption Then ' obtaining the clicked card Dim RowID As String = View.GetRowCellValue(Hi.RowHandle, _ View.Columns("ProductID")).ToString() GridControl1.DoDragDrop(RowID, DragDropEffects.Move) End If End Sub // ... Private Sub Form1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) _ Handles MyBase.DragOver Dim Grid As GridControl = sender ' obtaining the View over which the card is being dragged Dim View As BaseView = Grid.GetViewAt(New Point(e.X, e.Y)) If TypeOf View Is CardView Then e.Effect = DragDropEffects.Move Else e.Effect = DragDropEffects.None End If End Sub // ... Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) _ Handles MyBase.DragDrop Dim Grid As GridControl = sender ' obtaining the View to which the card has been dropped Dim pt As Point = GridControl1.PointToClient(New Point(e.X, e.Y)); Dim View As CardView = Grid.GetViewAt(pt) Dim dragData As String = e.Data.GetData(DataFormats.Text).ToString() Dim RowID As Integer = Convert.ToInt32(dragData) ' obtaining the row which owns the detail clone that the card has been dropped on Dim parentView As GridView = View.ParentView Dim cellValue As Object = parentView.GetRowCellValue(view.SourceRowHandle, _ parentView.Columns("CategoryID")) Dim currentParentID As Integer = Convert.ToInt32(cellValue) ' changing the parent row of the card results in moving this card to specified parent row: DataSet11.Products.Rows.Find( _ RowID)(DataSet11.Products.Columns("CategoryID")) = currentParentID End Sub |