【问题标题】:Get the position of a grid column获取网格列的位置
【发布时间】:2008-12-17 20:19:57
【问题描述】:

如何在表单中定位按钮,使其始终位于 DevExpress 网格中的列上方?

网格列已设置为无法调整大小,但网格和列会随表单调整大小。 System.Forms.Control 具有 PointToScreen method 看起来会提供此功能,但不是 DevExpress gridviewcolumn 中的方法。

【问题讨论】:

  • 你可以使用一个模板作为标题并在其中放置一个 DIV 标签,然后在 JavaScript 中获取该 div 标签的位置以获取屏幕上的位置吗?

标签: c# forms devexpress controls


【解决方案1】:

您可以找到相对于GridControl 的列坐标,然后转换为Form 坐标。为此,您可以使用可以从 GridViewInfo.ColumnsInfo 属性获取的 GridColumnsInfo 对象。要获取GridViewInfo 对象,您可以使用gridView.GetViewInfo() 方法。
找到坐标后,您将需要订阅在不同的 GridViewGridControl 更改时发生的事件。例如,您可以订阅GridView.LayoutGridView.LeftCoordChanged 事件。要调整大小,您需要订阅 GridControl.Resize 事件。

例子:

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

您可以为您订阅的每个事件调用此方法。

【讨论】:

  • 这就像一个梦想。谢谢。我删除了顶部的设置,因为我在网格上方的面板中布置了我的按钮。按钮像梦一样移动。
猜你喜欢
  • 2014-11-18
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多