5

我使用这个http://www.devexpress.com/Support/Center/p/Q233111.aspx(代码在 VB 中,所以我将其转换为 C#)来获取垂直列标题。我得到了垂直标题,但我的问题是其中一些不适合,所以它们不完全可见。

是否可以自动调整列标题?(所有高度设置为最大高度)

4

1 回答 1

4

Devexpress 支持中心所示,我认为这将是您问题的解决方案首先在您的解决方案中添加一个帮助程序类

public class AutoHeightHelper
{
    GridView view;
    public AutoHeightHelper(GridView view)
    {
        this.view = view;
        EnableColumnPanelAutoHeight();
    }

    public void EnableColumnPanelAutoHeight()
    {
        SetColumnPanelHeight();
        SubscribeToEvents();
    }

    private void SubscribeToEvents()
    {
        view.ColumnWidthChanged += OnColumnWidthChanged;
        view.GridControl.Resize += OnGridControlResize;
        view.EndSorting += OnGridColumnEndSorting;
    }

    void OnGridColumnEndSorting(object sender, EventArgs e)
    {
        view.GridControl.BeginInvoke(new MethodInvoker(SetColumnPanelHeight));
    }

    void OnGridControlResize(object sender, EventArgs e)
    {
        SetColumnPanelHeight();
    }

    void OnColumnWidthChanged(object sender, DevExpress.XtraGrid.Views.Base.ColumnEventArgs e)
    {
        SetColumnPanelHeight();
    }

    private void SetColumnPanelHeight()
    {
        GridViewInfo viewInfo = view.GetViewInfo() as GridViewInfo;
        int height = 0;
        for (int i = 0; i < view.VisibleColumns.Count; i++)
            height = Math.Max(GetColumnBestHeight(viewInfo, view.VisibleColumns[i]), height);
        view.ColumnPanelRowHeight = height;
    }

    private int GetColumnBestHeight(GridViewInfo viewInfo, GridColumn column)
    {
        GridColumnInfoArgs ex = viewInfo.ColumnsInfo[column];
        GraphicsInfo grInfo = new GraphicsInfo();
        grInfo.AddGraphics(null);
        ex.Cache = grInfo.Cache;
        bool canDrawMore = true;
        Size captionSize = CalcCaptionTextSize(grInfo.Cache, ex as HeaderObjectInfoArgs, column.GetCaption());
        Size res = ex.InnerElements.CalcMinSize(grInfo.Graphics, ref canDrawMore);
        res.Height = Math.Max(res.Height, captionSize.Height);
        res.Width += captionSize.Width;
        res = viewInfo.Painter.ElementsPainter.Column.CalcBoundsByClientRectangle(ex, new Rectangle(Point.Empty, res)).Size;
        grInfo.ReleaseGraphics();
        return res.Height;
    }

    Size CalcCaptionTextSize(GraphicsCache cache, HeaderObjectInfoArgs ee, string caption)
    {
        Size captionSize = ee.Appearance.CalcTextSize(cache, caption, ee.CaptionRect.Width).ToSize();
        captionSize.Height++; captionSize.Width++;
        return captionSize;
    }

    public void DisableColumnPanelAutoHeight()
    {
        UnsubscribeFromEvents();
    }

    private void UnsubscribeFromEvents()
    {
        view.ColumnWidthChanged -= OnColumnWidthChanged;
        view.GridControl.Resize -= OnGridControlResize;
        view.EndSorting -= OnGridColumnEndSorting;
    }
}

然后在您的表单上,您应该通过添加以下代码行使帮助类来处理 GridView 的列调整事件大小

AutoHeightHelper helper;
private void OnFormLoad(object sender, EventArgs e)
{
    helper = new AutoHeightHelper(gridView1);
    helper.EnableColumnPanelAutoHeight();
}

private void OnFormClosing(object sender, FormClosingEventArgs e)
{
    helper.DisableColumnPanelAutoHeight();
}

希望这可以帮助...

于 2011-09-29T09:11:38.970 回答