Auto-size entire row height.
It is common to use the rows auto-size functionality when the cells have multi-line text. However by default the grid measures only the visible cells text. So when the users scrolls horizontally the row height might be adjusted dynamically. This article will explain how you can measure the entire row so the row height is constant while the users are scrolling horizontally.
Create custom row element.
In order to return the proper rows height to the grid you need to create a custom row element. Then you should override the MeasureOverride method. You need this method in order to measure all cells not just the visual ones. Then you can return the proper height. In order to speed the measure process, you can check the height only of the cells that are not visible.
Sample implementation for the custom row element.
public class CustomRowElement : GridDataRowElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridDataRowElement);
}
}
protected override SizeF MeasureOverride(SizeF availableSize)
{
SizeF baseSize = base.MeasureOverride(availableSize);
CellElementProvider provider = new CellElementProvider(this.TableElement);
SizeF desiredSize = SizeF.Empty;
foreach (GridViewColumn column in this.ViewTemplate.Columns)
{
if (!this.IsColumnVisible(column))
{
continue;
}
GridDataCellElement cellElement = provider.GetElement(column, this) as GridDataCellElement;
this.Children.Add(cellElement);
if (cellElement != null)
{
cellElement.Measure(new SizeF(column.Width, float.PositiveInfinity));
if (desiredSize.Height < cellElement.DesiredSize.Height)
{
desiredSize.Height = cellElement.DesiredSize.Height;
}
}
cellElement.Detach();
this.Children.Remove(cellElement);
}
SizeF elementSize = this.TableElement.RowScroller.ElementProvider.GetElementSize(this.RowInfo);
int oldHeight = RowInfo.Height == -1 ? (int)elementSize.Height : RowInfo.Height;
this.RowInfo.SuspendPropertyNotifications();
this.RowInfo.Height = (int)desiredSize.Height;
this.RowInfo.ResumePropertyNotifications();
if (!this.RowInfo.IsPinned)
{
int delta = RowInfo.Height - oldHeight;
TableElement.RowScroller.UpdateScrollRange(TableElement.RowScroller.Scrollbar.Maximum + delta, false);
}
baseSize.Height = this.RowInfo.Height;
return baseSize;
}
private bool IsColumnVisible(GridViewColumn column)
{
return column.IsVisible;
}
}Using the custom row element.
The final step is to replace the default row elements. This can be achieved in the CreateRow event handler.
void radGridView1_CreateRow(object sender, GridViewCreateRowEventArgs e)
{
if (e.RowType == typeof(GridDataRowElement))
{
e.RowType = typeof(CustomRowElement);
}
}A complete solution which adds cached height is available in our SDK repository.