System.NullReferenceException on calling Grid.BestFitColumns

4 posts, 1 answers
  1. Czeshirecat
    Czeshirecat avatar
    66 posts
    Member since:
    Jun 2016

    Posted 13 Mar 2019 Link to this post

    Version 2019.1.219

    I've been receiving the above exception on my grids when they're initially filled with their first page of data and on first call to BestFitColumns.

    When tracing into decompiled source, the error is thrown on the function in StretchVirtualGridColumnLayout.cs

    /// <summary>Starts the column resize.</summary>
        /// <param name="column">The column.</param>
        public override void StartColumnResize(int column)
        {
          if (!this.stretchableColumns.Contains(column))
            return;

     

    stretchableColumns are null at this moment which is what's throwing the exception.

    Investigating further by placing some break points, the above function is being called before the function "public override void CalculateColumnWidths(SizeF availableSize)" which actually generates the stretchableColumns list.

    Im sorry, but I can't include a sample project at this time as the code is too complex.

    The grid.OnCellValueNeeded event drives obtaining the data.

    I'm not sure where I should call the BestFitColumns method. I am receiving the exception in the OnCellNeeded and OnCellFormatting events on the first call.

    Can you advise please?

     

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2926 posts

    Posted 14 Mar 2019 Link to this post

    Hi Claire,

    These events are not suitable for calling the best fit method. They are fired many times for each cell and the grid is not loaded at this point. You can override the OnLoad or the OnShown methods in the form and call the method after the grid is fully loaded. For example:
    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        radVirtualGrid1.BestFitColumns();
    }

    Let me know how this works for you.

    Regards,
    Dimitar
    Progress Telerik
    Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Czeshirecat
    Czeshirecat avatar
    66 posts
    Member since:
    Jun 2016

    Posted 14 Mar 2019 in reply to Dimitar Link to this post

    thanks for clarifying that :)
    I did have a feeling it was something to do with this as my data load event is usually triggered on a grid page change.

    It's just the initial read that I wanted to leave until I was sure that everything to that moment had been initialized. I'll try reorganizing.

  4. Dimitar
    Admin
    Dimitar avatar
    2926 posts

    Posted 14 Mar 2019 Link to this post

    Hello Claire,

    If you are using paging you can use the PageChanged event to call the method:
    private void RadVirtualGrid1_PageChanged(object sender, Telerik.WinControls.UI.VirtualGridEventArgs e)
    {
        radVirtualGrid1.BestFitColumns();
    }

    Do not hesitate to contact us if you have other questions.
     
    Regards,
    Dimitar
    Progress Telerik
    Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top