Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Infragistics UltraGrid to fit its content perfectly

How do I resize the entire ultragrid control in code to only show its contents? No blank space ( problem is that data table inside is sometimes small sometimes big)

I have:

-----------------------------
|   |   |    |    blank      |
|   |   |    |               |
|   |   |    |               |
| ------------      blank    |
|                            |
|   blank                    |
|____________________________|

and I want the borders to be tight to the grid. I tried: a) read DefaultLayout.Bands(0).GetExtent() ~ gives always the same number b) read data source size/height and ~ gives always the same number

Do I need to handle some change layout events? Or this is some property file of grid that needs to be handled?

like image 307
Antea Avatar asked Nov 10 '22 00:11

Antea


1 Answers

Determining the width in any arbitrary case is extremely difficult, because the grid has so many options. Multiple bands, RowLayouts, overlapping borders, scrollbars, card view, etc.

In the simplest case with a single band, the GetExtent method on the band should get you most of the way there. If it's not working for you, then my guess is that it's a problem with timing. The grid does a lot of metrics calculations asynchronously, so you need to make sure the grid paints so that all calculations are performed before you try to use GetExtent or other measurements.

Then you just have to account for Scrollbars and borders which can be done via the UIElements.

    private void AutoSizeGridToContents(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid)
    {
        UltraGridLayout layout = ultraGrid.DisplayLayout;
        UltraGridBand rootBand = layout.Bands[0];
        int extent = rootBand.GetExtent(BandOrigin.PreRowArea);

        UIElement gridElement = layout.UIElement;
        UIElementBorderStyle borderStyle = gridElement.BorderStyle;
        Border3DSide borderSides = gridElement.BorderSides;
        UIElementBorderWidths borderWidths = DrawUtility.CalculateBorderWidths(borderStyle, borderSides, gridElement);
        extent += borderWidths.Left + borderWidths.Right;

        UIElement scrollbarUIElement = gridElement.GetDescendant(typeof(RowScrollbarUIElement));
        if (null != scrollbarUIElement)
            extent += scrollbarUIElement.Rect.Width;

        ultraGrid.Width = extent;
    }

Like I said, this works in the simplest case with just a single band.

like image 160
Mike Avatar answered Nov 14 '22 23:11

Mike