admin管理员组

文章数量:1419214

I’m working with the TelerikGrid in Blazor and trying to apply AutoFitColumnsAsync directly when the grid initializes. My goal is to have the column widths automatically adjusted as soon as the grid is rendered for the first time.

I’ve tried several approaches, including:

  • Calling AutoFitAllColumnsAsync() inside OnInitialized or OnAfterRenderAsync.

  • Using JavaScript along with IJSRuntime and [JSInvokable] to trigger the autofit functionality.

    However, the column autofit only works when I manually trigger it, such as by clicking a button. I cannot get it to work automatically during the grid’s initialization.

private async Task AutoFitAllColumns()
   {
       
       
      await GridRef.AutoFitColumnAsync("nameColumn");
       
   }


   protected override async Task OnAfterRenderAsync(bool firstRender)
   {
       if (firstRender && !isGridInitialized)
       {
           isGridInitialized = true;

           // Führe AutoFit drei Mal aus, nachdem das Grid gerendert wurde
           await AutoFitAllColumns();
           await AutoFitAllColumns();
           await AutoFitAllColumns();
       }
   }

It seems like the grid needs to be fully rendered before AutoFitAllColumnsAsync() works correctly. Is there a way to ensure that the columns autofit as part of the initialization process, without needing to trigger it manually (e.g., with a button click)?

Any suggestions or examples would be greatly appreciated! Thank you in advance.

本文标签: telerikHow to AutoFitColumns on TelerikGrid Initialization in BlazorStack Overflow