Hit Information

  • 4 minutes to read

Online Video

  • Hit Information

    This tutorial shows how to use hit information to complete the following tasks:

    • display tooltips that indicate which element is under the mouse cursor;
    • apply/clear filters on a cell click.

    Watch the Video | Read the Walkthrough

 

To identify a View's visual element at specific coordinates, call the View's CalcHitInfo method.

These methods accept a point measured in grid control client coordinates and return HitInfo objects with information that identifies the test point. Different View types return different HitInfo objects. For example, a GridView returns a GridHitInfo object, a LayoutView returns a LayoutViewHitInfo object, etc.


Point clientPt = gridControl1.PointToClient(Cursor.Position);
GridHitInfo ghi = gridView1.CalcHitInfo(clientPt);
//...

Test if a point belongs to a particular visual element

To identify whether a point belongs to a certain visual element (for example, column header filter button, row preview region, row/column edge, row cell), read the HitInfo.HitTest enumeration property (for instance, GridHitTest enumerates all visual elements in the GridView class).


if (ghi != null) {
    Text = ghi.HitTest.ToString();
    if(ghi.HitTest == GridHitTest.ColumnFilterButton) {
        //...
    }
}

The HitInfo objects also provide helper properties to identify complex visual elements (for example, a row, data row, column header panel, etc.). These properties have the "In..." prefix. For example:

  • HitInfo.InRow returns whether the test point belongs to the 'row' visual element. This complex element includes row cells, row preview region, indicator button, edges, group button (for group rows), etc.
  • HitInfo.InColumn returns true if the test point belongs to the column header button or the column header filter button.

if(ghi.InRow) {
    myMenu.Show();
}

Identify the row/column/band/card

To identify the column, row, and band (when applicable) that contain the test point, use the corresponding properties the HitInfo objects provide: HitInfo.Column, HitInfo.RowHandle and HitInfo.Band.


//Test if the point belongs to the focused row
if(ghi.RowHandle == ghi.View.FocusedRowHandle) {
    //...
}

To determine which View is at the specified coordinates when the GridControl displays multiple Views, call the GridControl.GetViewAt method.

Example

The following sample code shows how to identify the element located at a specific point using the GridView.CalcHitInfo method.

In the example, the CalcHitInfo method is called when you move the cursor over a Grid Control. The name of the current View element is displayed in the form's caption.

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
// ...
private void gridControl1_MouseMove(object sender, MouseEventArgs e) {
    GridControl grid = sender as GridControl;
    if (grid == null) return;
    // Get a View at the current point.
    BaseView view = grid.GetViewAt(e.Location);
    if (view == null) return;
    // Retrieve information on the current View element.
    BaseHitInfo baseHI = view.CalcHitInfo(e.Location);
    GridHitInfo gridHI = baseHI as GridHitInfo;
    if (gridHI != null)
        Text = gridHI.HitTest.ToString();
}

Example

This example shows how to display a context menu when an end-user right-clicks the Header Panel Button. The created context menu contains a "Column Auto Width" check box. This menu item toggles the GridOptionsView.ColumnAutoWidth option.

GridView-Menu-For-Column-Header-Panel-Button

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 (hitInfo.HitTest != GridHitTest.ColumnButton) return;
    ViewMenu menu = new ViewMenu(view);
    menu.Items.Add(CreateCheckMenuItemColumnAutoWidth(view));
    menu.Show(hitInfo.HitPoint);
}

DXMenuCheckItem CreateCheckMenuItemColumnAutoWidth(GridView view) {
    DXMenuCheckItem checkItem = new DXMenuCheckItem("Column &Auto Width",
      view.OptionsView.ColumnAutoWidth, null, new EventHandler(OnColumnAutoWidthMenuItemClick));
    checkItem.Tag = view;
    return checkItem;
}

void OnColumnAutoWidthMenuItemClick(object sender, EventArgs e) {
    DXMenuCheckItem item = sender as DXMenuCheckItem;
    GridView view = item.Tag as GridView;
    if (view == null) return;
    view.OptionsView.ColumnAutoWidth = item.Checked;
    if (!view.OptionsView.ColumnAutoWidth)
        view.BestFitColumns();
}
See Also