Get grid column position

How can I put a button on a form so that it is always above the column in the DevExpress grid?

The columns of the grid are configured so that they cannot be changed, but the grid and columns are reformatted using the form. System.Forms.Control has a PointToScreen method that looks like it will provide this functionality, but not a method in gridviewcolumn DevExpress.

+3
source share
1 answer

You can find the coordinates of the column relative to GridControland then translate into coordinates Form. For this, you can use an object GridColumnsInfothat you can get from the property GridViewInfo.ColumnsInfo. To get an object GridViewInfo, you can use a method gridView.GetViewInfo().
When you find the coordinates, you will need to subscribe to events that occur during various changes GridViewand GridControl. For example, you can subscribe to GridView.Layoutand GridView.LeftCoordChangedevents. To resize, you need to subscribe to the event GridControl.Resize.

Example:

private void UpdatePosition(GridView gridView, string columnName, Control control)
{
    var column = gridView.Columns[columnName];

    if (column == null) return;

    var viewInfo = (GridViewInfo)gridView.GetViewInfo(); //using DevExpress.XtraGrid.Views.Grid.ViewInfo
    var columnInfo = viewInfo.ColumnsInfo[column];

    if (columnInfo != null)
    {
        var bounds = columnInfo.Bounds; //column rectangle of coordinates relative to GridControl

        var point = PointToClient(gridView.GridControl.PointToScreen(bounds.Location)); //translating to form coordinates

        control.Left = point.X;
        control.Top = point.Y - control.Height;
        control.Width = bounds.Width;

        control.Show();
    }
    else
        control.Hide();
}

You can call this method for every event you register.

+2
source

All Articles