Binding with array

hi, i have this problem with sfgrid in blazor webassembly component.

I have this class: 

myclass1

{

public string Code {get;set;} 

public string Color{get;set;}

}

 and this class 

myclass2

{

public myclass1[] Description {get;set;} 

public string Value {get;set;}

public string OtherValue {get;set;}

}

I then have this variable 

list<myclass2> mylist 

which I insert as the datasource of my sfgrid in this code

<SfGrid @ref="@dglist" DataSource="@mylist" Tool="@(new List<string>() { "Print" })" Height="100%" Width="100%" EnableStickyHeader="true">

 <GridColumns>

 <GridColumn Width="200px"> 

<HeaderTemplate> Value<br /> Othervalue </HeaderTemplate>

 <Template> @{ var t = (context as myclass2);

 <label>@t.Value</label> <br /> <label>@t.OtherValue</label> } </Template> 

</GridColumn> 

@for (int i = 1; i <= 10; i++) {

 <GridColumn HeaderText="@i.ToString()"> 

<Template> @{ var t = (context as myclass2);

 @if (t != null && t.Description!=null) { @t.Description?[i].Code} } </Template> 

</GridColumn> }

 </GridColumns> 

</SfGrid>

I load the data correctly in the OnAfterRenderAsync override method 

(I tried outside the grid and they are shown regularly)

Now when I run I get this error in the browser:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]

Unhandled exception rendering component: Object reference not set to an instance of an object.

System.NullReferenceException: Object reference not set to an instance of an object. at Sid.Client.Forms.Planning.<>c__DisplayClass0_1.<BuildRenderTree>b__6(RenderTreeBuilder __builder5) in C:\VSP\VS2022\Net8\Sid\Sid.Client\Forms\Planning.razor:line 55

   at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)

   at Syncfusion.Blazor.Grids.Internal.GridCell`1[[Sid.Common.myclass2, Sid.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].BuildRenderTree(RenderTreeBuilder __builder)

   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)

   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException) 

and the gridcell with numeric header are empty.

What should I do to solve this problem? 

thanks





3 Replies

NP Naveen Palanivel Syncfusion Team September 16, 2024 05:36 PM UTC

Hi egidio,

Based on your query, it seems that you're encountering a null reference exception at runtime. We have prepared a simple sample based on the code snippet provided in the forum. Please review the sample for further details.

Sample : https://blazorplayground.syncfusion.com/embed/VXLptYCxoCxYfoSN?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5

If the reported issue still reproduced then kindly share the below details to validate further at our end.


  1. Share us the entire Grid code snippet
  2. Please share if we missed any replication procedure in attached sample
  3. Share a video demonstration of the issue with a detailed explanation. This will greatly assist us in understanding the problem.
  4. Please provide a simple sample that reproduces the issue with duplicate data or try to modify the above mentioned sample.


The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.


Regards,

Naveen Palanivel



EG egidio September 26, 2024 09:06 AM UTC

the code you proposed works but if I replace

<Template>

@{

var t = (context as myclass2);

@if (t != null && t.Description != null)

{

@t.Description?[i].Code

}

}

</Template>


with


<Template>

@{

var t = (context as myclass2);

<mycel Myparameter="@t.Description[@i]" />

}

</Template>

all instances of mycel contain the same data even if t.Description is different 

where mycel is a component composed of a button.

I solved it by replicating 10 times

<Template>

@{

var t = (context as myclass2);

<mycel Myparameter="@t.Description[1]" />

}

</Template>

<Template>

@{

var t = (context as myclass2);

<mycel Myparameter="@t.Description[2]" />

}

</Template>

.

.

etc.



PS Prathap Senthil Syncfusion Team September 27, 2024 01:10 PM UTC

Hi Egidio,

Before proceeding with the issue you reported, we need some additional clarification from you. Please provide the following details so we can assist you further:

    • Could you provide more details about the mycel component? Specifically, how does it handle the Myparameter value that is passed to it? There may be an issue with the way data is being passed and rendered in the component.
    • Could you please provide a simple sample that replicates the issue with duplicate data, or try to reproduce the issue using the attached sample?

The information you provide will be very helpful for us to validate your query and provide a solution as quickly as possible.


Regards,
Prathap Senthil


Loader.
Up arrow icon