@@ -66,8 +66,7 @@ The known limitations of the Autofit Columns feature include:
6666> caption Grid Column Resizing and Autofitting
6767
6868```` CSHTML
69- @* Grid column resizing and autofitting *@
70- @* Drag the border between column headers to change the column width. The command column is not resizable by the user. *@
69+ <p>Resize the Grid columns and click the AutoFit buttons. The command column is not resizable by the user.</p>
7170
7271<TelerikButton OnClick="@AutoFitSingleColumn">AutoFit ID Column</TelerikButton>
7372<TelerikButton OnClick="@AutoFitMultipleColumns">AutoFit String Columns</TelerikButton>
@@ -76,38 +75,43 @@ The known limitations of the Autofit Columns feature include:
7675<TelerikGrid @ref="@GridRef"
7776 Data="@GridData"
7877 Resizable="true"
79- Pageable="true" PageSize="10" Sortable="true" Height="300px">
78+ Pageable="true"
79+ Sortable="true"
80+ Height="300px">
8081 <GridColumns>
81- <GridColumn Field=@nameof(SampleData.Id) Title="ID" Id="IDColumn " />
82- <GridColumn Field=@nameof(SampleData.Name ) Title="First Name" Id="NameColumn1 " />
83- <GridColumn Field=@nameof(SampleData.LastName) Title="Last Name" Id="NameColumn2 " />
82+ <GridColumn Field=@nameof(SampleData.Id) Title="ID" Id="@IdColumnId " />
83+ <GridColumn Field=@nameof(SampleData.FirstName ) Title="First Name" Id="@FirstNameColumnId " />
84+ <GridColumn Field=@nameof(SampleData.LastName) Title="Last Name" Id="@LastNameColumnId " />
8485 <GridCommandColumn Width="100px" Resizable="false">
85- <GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true">Update</GridCommandButton>
86- <GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
87- <GridCommandButton Command="Delete" Icon="@SvgIcon.Trash">Delete</GridCommandButton>
88- <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton>
86+ <GridCommandButton Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
87+ <GridCommandButton Icon="@SvgIcon.Trash">Delete</GridCommandButton>
8988 </GridCommandColumn>
9089 </GridColumns>
9190</TelerikGrid>
9291
9392@code {
94- public TelerikGrid<SampleData> GridRef { get; set; }
95- public List<SampleData> GridData { get; set; }
93+ public TelerikGrid<SampleData>? GridRef { get; set; }
94+ public List<SampleData> GridData { get; set; } = new();
95+
96+ // Columns IDs used in the Grid column definitions and in the AutoFit methods.
97+ private const string IdColumnId = "id-column";
98+ private const string FirstNameColumnId = "first-name-column";
99+ private const string LastNameColumnId = "last-name-column";
96100
97101 private async Task AutoFitSingleColumn()
98102 {
99- await GridRef.AutoFitColumnAsync("IDColumn" );
103+ await GridRef! .AutoFitColumnAsync(IdColumnId );
100104 }
101105
102106 private async Task AutoFitMultipleColumns()
103107 {
104- var columns = new List<string>() { "NameColumn1", "NameColumn2" };
105- await GridRef.AutoFitColumnsAsync(columns );
108+ var columnIds = new List<string>() { FirstNameColumnId, LastNameColumnId };
109+ await GridRef! .AutoFitColumnsAsync(columnIds );
106110 }
107111
108112 private async Task AutoFitAllColumns()
109113 {
110- await GridRef.AutoFitAllColumnsAsync();
114+ await GridRef! .AutoFitAllColumnsAsync();
111115 }
112116
113117 protected override void OnInitialized()
@@ -120,16 +124,16 @@ The known limitations of the Autofit Columns feature include:
120124 return Enumerable.Range(1, 50).Select(x => new SampleData
121125 {
122126 Id = x,
123- Name = $"name {x}",
127+ FirstName = $"Name {x}",
124128 LastName = $"Surname {x}"
125129 }).ToList();
126130 }
127131
128132 public class SampleData
129133 {
130134 public int Id { get; set; }
131- public string Name { get; set; }
132- public string LastName { get; set; }
135+ public string FirstName { get; set; } = string.Empty;
136+ public string LastName { get; set; } = string.Empty;
133137 }
134138}
135139````
0 commit comments