Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoheight of vertical column header in C# using DevXpress

I used this http://www.devexpress.com/Support/Center/p/Q233111.aspx (the code is in VB so I converted it to C#) to get vertical column headers. I get the vertical headers but my problem is some of them doesn't fit, so they are not fully visible.

Is it possible to autoheight the column headers ? (all height set to max height)

like image 797
Eternal Noob Avatar asked Oct 09 '22 22:10

Eternal Noob


1 Answers

As shown in Devexpress support center i think this will be the solution to your problem First add an helper class to your solution

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

Then on your form you should make the helper class to Handle GridView's columns resize events by adding the following lines of code

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

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

Hope this helps...

like image 105
Ginka Avatar answered Oct 13 '22 11:10

Ginka