AutoSizeRows

2 posts, 1 answers
  1. Mohsen
    Mohsen avatar
    5 posts
    Member since:
    Jun 2014

    Posted 19 Nov 2015 Link to this post

    I need something like BesFitColumns() for rows. I know that I can use AutoSizeRows, but it has problem with cells that have long multiline content. I attached screenshot. Please check it.
  2. Answer
    Dess
    Admin
    Dess avatar
    1605 posts

    Posted 23 Nov 2015 Link to this post

    Hello Mohsen,

    Thank you for writing.

    Due to UI virtualization in RadGridView, auto-size rows mechanism takes into consideration the currently visible cells. That is why when a multiline text is not visible, the row's height is small, but when you show the multiline text while scrolling, the height is adjusted to display the whole cell's content. This is normal behavior. However, you can turn off the auto-size mechanism and measure all cells in a row in order to determine the row's height considering all cells(including the invisible ones as well). You can find below a sample implementation:
    private void Form1_Load(object sender, EventArgs e)
    {
        this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
        this.radGridView1.BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells);
        foreach (GridViewColumn col in this.radGridView1.Columns)
        {
            col.WrapText = true;
        }
        this.radGridView1.AutoSizeRows = false;
     
        AdjustRowHeight();
        this.radGridView1.Columns["CompanyName"].IsPinned = true;
        this.radGridView1.Columns["Phone"].IsPinned = true;
    }
     
    private void AdjustRowHeight()
    {
        RowElementProvider rowProvider = (RowElementProvider)this.radGridView1.TableElement.RowElementProvider;
        CellElementProvider cellProvider = (CellElementProvider)this.radGridView1.TableElement.CellElementProvider;
          
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            row.Height = (int)GetRowHeight(this.radGridView1, rowProvider, cellProvider, row);
        }
    }
     
    public double GetRowHeight(RadGridView radGridView, RowElementProvider rowProvider, CellElementProvider cellProvider,
        GridViewRowInfo gridViewRowInfo)
    {
        double rowHeight = 0;
     
        GridRowElement visualRow = rowProvider.GetElement(gridViewRowInfo, null) as GridRowElement;
        visualRow.InitializeRowView(radGridView.TableElement);
        visualRow.Initialize(gridViewRowInfo);
        radGridView.TableElement.Children.Add(visualRow);
     
        foreach (GridViewColumn col in visualRow.ViewTemplate.Columns)
        {
            if (col is GridViewRowHeaderColumn || col is GridViewIndentColumn)
            {
                continue;
            }
     
            GridCellElement cell = cellProvider.GetElement(col, visualRow) as GridCellElement;
            visualRow.Children.Add(cell);
     
            cell.Initialize(col, visualRow);
            cell.SetContent();
            cell.UpdateInfo();
     
            GridHeaderCellElement headerCell = cell as GridHeaderCellElement;
     
            if (headerCell != null)
            {
                headerCell.UpdateArrowState();
            }
     
            cell.ResetLayout(true);
            cell.Measure(new System.Drawing.SizeF(cell.ColumnInfo.Width, float.PositiveInfinity));
            rowHeight = Math.Max(rowHeight, cell.DesiredSize.Height);
     
            ReleaseCellElement(cellProvider, visualRow, cell);
        }
     
        ReleaseRowElement(radGridView, rowProvider, visualRow);
     
        return rowHeight;
    }
     
    void ReleaseRowElement(RadGridView radGridView, RowElementProvider rowProvider, GridRowElement rowElement)
    {
        rowProvider.CacheElement(rowElement);
        rowElement.Detach();
        radGridView.TableElement.Children.Remove(rowElement);
        rowElement.ResumeLayout(false);
    }
     
    void ReleaseCellElement(CellElementProvider cellProvider, GridRowElement rowElement, GridCellElement cell)
    {
        GridVirtualizedCellElement virtualizedCell = cell as GridVirtualizedCellElement;
        if (virtualizedCell != null)
        {
            cellProvider.CacheElement(virtualizedCell);
            virtualizedCell.Detach();
            rowElement.Children.Remove(cell);
        }
        else
        {
            cell.Dispose();
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top