This is a migrated thread and some comments may be shown as answers.

System.NullReferenceException on calling Grid.BestFitColumns

3 Answers 63 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Czeshirecat
Top achievements
Rank 2
Iron
Iron
Czeshirecat asked on 13 Mar 2019, 04:12 PM

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?

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 14 Mar 2019, 09:35 AM
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.
0
Czeshirecat
Top achievements
Rank 2
Iron
Iron
answered on 14 Mar 2019, 09:45 AM

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.

0
Dimitar
Telerik team
answered on 14 Mar 2019, 12:25 PM
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.
Tags
VirtualGrid
Asked by
Czeshirecat
Top achievements
Rank 2
Iron
Iron
Answers by
Dimitar
Telerik team
Czeshirecat
Top achievements
Rank 2
Iron
Iron
Share this question
or