Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1330
Preventing columns from moving outside the width of the UltraGrid
posted

If:

this.DisplayLayout.AutoFitStyle = AutoFitStyle.None;

Then when the user resizes a column so that it is very large, the result is that columns get pushed outside the bounds of the control and scroll arrows appear.

I want to restrict this behaivor.  I essentially want the ultra grid to behave as the microsoft Data Grid View.  Where the grid will not allow the user to resize a column so large that it pushes others out of view.

  • 469350
    Offline posted

    Hi,

    If you don't want a horizontal scrollbar to ever be needed in the grid, then you should use one of the other AutoFitStyles that fit the columns into the available width.

    There's no built-in way to limit the column widths to the available area without doing that.

    If you really want to work at it, you can probably limit the users ability to increase the column widths past the available width using the BeforeColPosChanged event. But this is not a trivial undertaking.

    Here's some sample code that seems to work okay for me. But I'm sure this code is not complete. It will only work for the root band, and I didn't account for what happens when the grid is resized smaller.


            private void ultraGrid1_BeforeColPosChanged(object sender, BeforeColPosChangedEventArgs e)
            {
                // Watch for a column resize.
                if (e.PosChanged == PosChanged.Sized)
                {               
                    UltraGrid grid = (UltraGrid)sender;
                    UIElement gridElement = grid.DisplayLayout.UIElement;

                    // Get the RowColRegionIntersectionUIElement. This is the element that contains
                    // the columns, rows and cell, and does not include the scrollbars or the grid's
                    // borders.
                    UIElement rowColRegionIntersectionUIElement = gridElement.GetDescendant(typeof(RowColRegionIntersectionUIElement));

                    // Test some assumptions.
                    if (rowColRegionIntersectionUIElement == null)
                    {
                        Debug.Fail("Failed to find a RowColRegionIntersectionUIElement; unexpected.");
                        return;
                    }

                    if (e.ColumnHeaders.Length != 1)
                    {
                        Debug.Fail("More than one column headers is being resized; unexpected.");
                        return;
                    }

                    // Use the rowColRegionIntersectionUIElement to determine the total available
                    // width.
                    int totalAvailableWidth = rowColRegionIntersectionUIElement.Rect.Width;
                    int totalColumnWidthOriginal = 0;

                    // Determine the total width of all of the columns.
                    UltraGridBand band = grid.DisplayLayout.Bands[0];
                    foreach (UltraGridColumn column in band.Columns)
                    {
                        totalColumnWidthOriginal += column.Width;                  
                    }

                    // Subtract to determine how much empty width is available.
                    int remainingAvailableWidth = totalAvailableWidth - totalColumnWidthOriginal;

                    // Get the column that is being resized.
                    UltraGridColumn resizedColumn = e.ColumnHeaders[0].Column;               

                    // Determine the difference between the new size and the old size.
                    int resizeDifference = resizedColumn.Width - band.Columns[resizedColumn.Key].Width;

                    // If the difference in size is grater than the remaining empty width in the
                    // grid, then prevent the resize operation.
                    if (resizeDifference > remainingAvailableWidth)
                    {
                       e.Cancel = true;                   
                    }               
                }          
            }