How to Auto-Size RadGridView's Rows
Environment
Product Version | 2019.1.220 |
Product | RadGridView for WPF |
Description
How to auto-size the rows of the RadGridView control based on its height.
Solution
-
Define the following method in the code-behind of the control which holds your RadGridView instance:
What this does is to get the actual height of the GridViewScrollViewer - which is the element that holds all the rows of the control - and subtract from it the height of the header row element. This height is then divided by the total row count.private void AdjustRowHeight() { var scrollViewer = this.radGridView.ChildrenOfType<GridViewScrollViewer>().First(); double rowHeight = (scrollViewer.ActualHeight - scrollViewer.HeaderRow.ActualHeight) / this.radGridView.Items.Count; if (rowHeight > 0) { this.radGridView.RowHeight = rowHeight; } }
-
Call the AdjustRowHeight method in the Loaded and SizeChanged events of the RadGridView control:
private void RadGridView_Loaded(object sender, RoutedEventArgs e) { this.AdjustRowHeight(); } private void RadGridView_SizeChanged(object sender, SizeChangedEventArgs e) { this.AdjustRowHeight(); }
See Also
- ChildrenOfType