This is a migrated thread and some comments may be shown as answers.

AutoSizeRows

1 Answer 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mohsen
Top achievements
Rank 1
Mohsen asked on 19 Nov 2015, 12:33 PM
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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Nov 2015, 02:44 PM
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
Tags
GridView
Asked by
Mohsen
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or