C# – Get the position of a grid column

c++controlsdevexpressforms

How can I position a button within a form so that it is always above a column in a DevExpress grid?

The Grid columns are set up so they cannot be resized, but the grid and columns are re-sized with the form. System.Forms.Control has the PointToScreen method that looks like would it provides this functionality, but not a method in a DevExpress gridviewcolumn.

Best Solution

You can find column coordinates relative to the GridControl and translate then to Form coordinates. For this you can use GridColumnsInfo object that you can get from GridViewInfo.ColumnsInfo property. To get GridViewInfo object you can use gridView.GetViewInfo() method.
Once you find the coordinates, you will need to subscribe to events that occurs at different GridView and GridControl changes. For example you can subscribe to GridView.Layout and GridView.LeftCoordChanged events. For resizing you need to subscribe to GridControl.Resize event.

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's rectangle of coordinates relative to GridControl

        var point = PointToClient(gridView.GridControl.PointToScreen(bounds.Location)); //translating to form's 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 that you subscribed.