Skip to main content
A newer version of this page is available.
All docs
V17.2

Samples of Using Hit Information

  • 6 minutes to read

By using hit information you can have complete control over the behavior of grid elements. The Hit Information Overview section describes how you can obtain hit information. This topic provides a number of samples, which demonstrate how to implement custom behavior for grid elements.

Implementing Custom Popup Menus

The following sample demonstrates how to implement the grid row’s popup menu. This menu has only one item, which deletes the row that the menu has been called for. The GridView.CalcHitInfo method is called to obtain a row handle. Focus is then moved to this row and the custom context menu called.


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;
    // 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);
}

The screenshot below shows the result of executing the above code.

RowMenu

Implementing Custom Behavior Within Views

The following example assumes that we have a master-detail relationship within the grid control. The master View is represented by the GridView and the detail clones are represented by instances of the CardView class. The example below shows how to implement drag-and-drop operations between such detail clones.

The System.Windows.Forms.MouseDown event is used to identify the clicked card. For this purpose, the CardView.CalcHitInfo method is called. Then, the CardHitInfo.HitTest property is used to determine if the clicked point belongs to a card caption area and if so, dragging is started by calling the DoDragDrop method. The System.Windows.Forms.DragOver event is used to determine the position of the dragged card. The GridControl.GetViewAt method determines the View over which the card is currently being dragged. If the returned View is not a Card View, the dropping operation is prohibited.

Finally, the System.Windows.Forms.DragDrop event is handled to move the dragged card from its source clone to the target one. The GridControl.GetViewAt method is used to get the View to which the card has been dropped. The card is actually moved by changing the cell’s value within the column that refers to the master table.

Note: to enable dropping within the grid control you should set the AllowDrop property value to true.


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;       
}
See Also