You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: knowledge-base/grid-autofit-columns.md
+108-3Lines changed: 108 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
-
title: Autofit all Grid columns on initial load.
3
-
description: Autofit all Grid columns on initial page load, so that their widths match their content.
2
+
title: Autofit All Grid Columns on Initial Load
3
+
description: How to auto fit all Grid columns on initial page load, so that their widths match their content.
4
4
type: how-to
5
5
page_title: Autofit all Grid columns on initial page load
6
6
slug: grid-autofit-columns-on-initial-load
7
7
position:
8
8
tags: grid, autofit, columns
9
+
ticketid: 1568815
9
10
res_type: kb
10
11
---
11
12
@@ -20,6 +21,10 @@ In the 2.28.0 release of Telerik UI for Blazor, the Grid introduced [methods to
20
21
21
22
## Solution
22
23
24
+
There are two possible implementations to autofit Grid columns by default. They depend on whether the Grid is data-bound via [`Data` parameter](#data-parameter) or [`OnRead` event](#onread-event).
25
+
26
+
### Data Parameter
27
+
23
28
To AutoFit the Grid columns on initial load of the component you have to use a provision like the `MutationObserver`. This JavaScript tool notifies about DOM changes. The code snippet below uses the MutationObserver to trigger the `AutoFitAllColumns` method when the nodes in the content of Grid have mutated (rendered in this case).
24
29
25
30
Make sure to replace `<YOUR PROJECT NAMESPACE>` in the JavaScript code.
@@ -119,4 +124,104 @@ function observeTarget(gridClass) {
119
124
observer.disconnect();
120
125
}
121
126
}
122
-
````
127
+
````
128
+
129
+
### OnRead Event
130
+
131
+
Raise a boolean flag inside the `OnRead` handler, and then use it inside `OnAfterRenderAsync` to trigger column autofit. Note that you may need a small `Task.Delay()` inside `OnAfterRenderAsync`.
132
+
133
+
This approach is simpler than the above, because it doesn't require JavaScript and there is somewhat greater predictability when the Grid will render its rows.
134
+
135
+
>caption Autofit Grid columns when using OnRead data binding
136
+
137
+
````CSHTML
138
+
@using Telerik.DataSource
139
+
@using Telerik.DataSource.Extensions
140
+
141
+
<TelerikButton OnClick="@AutoFit">AutoFit All Columns Manually</TelerikButton>
0 commit comments