将行换行到下一行

时间:2015-10-12 10:03:08

标签: c# winforms infragistics ultragrid

我正在寻找一种方法将UltraGrid行换行到下一行,这样就不需要水平滚动条来查看整个网格。

在HTML / CSS中,它看起来像这样:https://jsfiddle.net/zjbyE/205/

我在DisplayLayoutOverride子对象中寻找可以做到这一点的任何属性都没有成功。也许我错过了明显的东西?

1 个答案:

答案 0 :(得分:1)

如果您正在寻找的话,网格没有任何方法可以自动包装单元格。换句话说,网格将不会基于与实际带宽相比的可用宽度来改变单元的位置。

但网格将允许您将单元格放置在数据行中的多个逻辑行上,理论上,您可以编写网格的Resize事件,以便在列不适合时重新定位列。

有两种方法可以在单个数据行中创建多个逻辑行。一种是使用列组,另一种是使用RowLayoutStyle.ColumnLayout。由于组需要组头,而你没有提到任何关于那些的东西,我将使用ColumnLayout方法,这有点复杂,但是当我在一个简单的单波段网格中尝试它时它工作得很好。

这是我掀起的一些示例代码。它并不完美,但它的效果非常好。

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            this.ultraDataSource1.Rows.Add(new object[] { i, i, i, i, i, i, i, i, i, i, i });
        }

        // Hook the paint event so we can wrap the cells the first time the grid paints. 
        // We can't do it here directly, because until the grid paints, there are no UIElements
        // and the column measurements may be wrong. 
        this.ultraGrid1.Paint += UltraGrid1_Paint;

    }

    private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
    {
        UltraGridLayout layout = e.Layout;
        UltraGridBand rootBand = layout.Bands[0];

        // ColumnLayout allows us to position the columns in a single data row on multiple
        // logical rows. 
        rootBand.RowLayoutStyle = RowLayoutStyle.ColumnLayout;

        // This code will be a lot more complex if we have to account for the possibility
        // that the vertical scrollbar may or may not be visible. So make it visible always
        // to spare us some headaches. 
        layout.Scrollbars = Scrollbars.Vertical;

        // This code doesn't account or multiple scroll regions to turn those off. 
        layout.MaxColScrollRegions = 1;
        layout.MaxRowScrollRegions = 1;
    }

    private void UltraGrid1_Paint(object sender, PaintEventArgs e)
    {
        // We only need to to this the first time, so unhook the Paint event. 
        this.ultraGrid1.Paint -= UltraGrid1_Paint;

        // Wrap the grid columns. 
        UltraGrid grid = (UltraGrid)sender;
        this.WrapGridRows(grid);
    }        

    private void ultraGrid1_Resize(object sender, EventArgs e)
    {
        // Wrap the grid columns. 
        UltraGrid grid = (UltraGrid)sender;
        this.WrapGridRows(grid);
    }

    private void WrapGridRows(UltraGrid grid)
    {
        UltraGridLayout layout = grid.DisplayLayout;

        // Determine the available Width of the grid.
        UIElement gridElement = layout.UIElement;
        UIElement rowColRegionIntersectionUIElement = gridElement.GetDescendant(typeof(RowColRegionIntersectionUIElement));
        int availableWidth = rowColRegionIntersectionUIElement.RectInsideBorders.Width;

        // X and y origins of each column in the ColumnLayout.
        int x = 0;
        int y = 0;

        // Keep track of the total width that has been used up on each row. 
        int totalUsedWidth = 0;

        // Loop through the columns.
        UltraGridBand band = layout.Bands[0];
        foreach (UltraGridColumn column in band.Columns)
        {
            // Get the width of the column. I added 2 here to account for borders. This doesn't seem
            // exactly right and I think there might be something else causing a slight shift here
            // but it's close enough.                 
            int cellWidth = column.CellSizeResolved.Width + 2;

            bool columnFitsOnCurrentLogicalRow = (totalUsedWidth + cellWidth) <= availableWidth;
            if (columnFitsOnCurrentLogicalRow)
            {
                // Position this column in the current row.                     
                column.RowLayoutColumnInfo.OriginX = x;
                column.RowLayoutColumnInfo.OriginY = y;     

                // Increment x to the next position.                
                x++;                    
            }
            else
            {
                // Reset x and y to the beginning of the next row. 
                x = 0;
                y++;

                // Position the column at the beginning of the next row. 
                column.RowLayoutColumnInfo.OriginX = x;
                column.RowLayoutColumnInfo.OriginY = y;

                // Increment x to the next position.
                x++;

                // Reset TotalUsedWidth since we are starting a new row. 
                totalUsedWidth = 0;
            }

            // The current column either got added to an existing row or a new row. 
            // Either way, we need to increase the total used width. 
            totalUsedWidth += cellWidth;

            // For reasons I won't go into here, the default Span is 2. Set it to 1 to 
            // make things simpler. 
            column.RowLayoutColumnInfo.SpanX = 1;
            column.RowLayoutColumnInfo.SpanY = 1;
        }
    }