Error while moving SfGrid components into different files

Hi Syncfusion team and community,

I have a blazor page with a grid control, I'm trying to separate the components that made a control in small blazor files to make the code more clean. Right now, I split successfuly the GridColumns component from SfGrid into another file, and everything runs fine.


The code version that works

[main_page.razor]

<SfGrid TValue="GridData">
    <SfDataManager AdaptorInstance="@typeof(custom_data_adaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager>
    <GridPageSettings PageSize="15"></GridPageSettings>
    <ColumnsComponent></ColumnsComponent>
</SfGrid>

[ColumnsComponent.razor]

<GridColumns>
    <GridColumn Field=@nameof(GridData.Value1) HeaderText="Value1"></GridColumn>
    <GridColumn Field=@nameof(GridData.Value2) HeaderText="Value2">
<Template>
@{
GridData data = (context as GridData);
<MyTooltipComponent Tooltip_string_value="@data.Value2" />
}
</Template>
</GridColumn>
</GridColumns>


The code version that doesn't work

But if I try to move from GridColumns to another file a GridColumn with a template in different files, it gives an error.

[main_page.razor]

<SfGrid TValue="GridData">
    <SfDataManager AdaptorInstance="@typeof(custom_data_adaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager>
    <GridPageSettings PageSize="15"></GridPageSettings>
    <ColumnsComponent></ColumnsComponent>
</SfGrid>

[ColumnsComponent.razor]

<GridColumns>
    <GridColumn Field=@nameof(GridData.Value1) HeaderText="Value1"></GridColumn>
<ColumnComponentWithTooltip TItem="GridData" Field_name="@GridData.Value2" Header_column_name="Value2"></ColumnComponentWithTooltip>
</GridColumns>

[ColumnComponentWithTooltip.razor] <-- THE ERROR OCCURS HERE

@typeparam TItem;

<GridColumn Field="@this.Field_name" HeaderText="@this.Header_column_name">
    <Template>
        @{
            TItem data = (context as TItem);
            Type data_type;
            PropertyInfo? property_info;
            string data_value;


            data_type = data.GetType();
            property_info = data_type.GetProperty(this.Field_name);
            data_value = property_info.GetValue(context);
            <MyTooltipComponent Tooltip_string_value="@data_value" />
}
    </Template>
</GridColumn>

@code{
[Parameter] public string Field_name { get; set; }
[Parameter] public string Header_column_name { get; set; }
}


It gives me the error inside the Template code from [ColumnComponentWithTooltip.razor].

Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type MyProject.Web ...\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_Templates_Grid_ColumnComponentWithTooltip_razor.g.cs 217 Active


I think that I lose the context object from the template, but I'm not sure. I hope you can help me to see the problem.


Thanks.


3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team May 30, 2022 11:26 AM UTC

Hi Antonio,                              


Greetings from Syncfusion support.


We suggest you to create the custom GridColumn component by inheriting from GridColumn. We have also attached a working sample in this ticket.


Please refer and use as like the below code to achieve this requirement.


 

[ColumnComponentWithTooltip.razor]

 

@using ServerApp.Data

@using System.Reflection

 

@typeparam TItem;

@inherits GridColumn   //inherit GridColumn

 

 

    <CascadingValue Value="@this" IsFixed="true">

        @ChildContent

    @{

        Template = context =>

        @: @{

                var data = (TItem)context;

                Type data_type;

                PropertyInfo? property_info;

                string data_value;

 

 

                data_type = data.GetType();

                property_info = data_type.GetProperty(this.Field_name);

                data_value = property_info.GetValue(context).ToString();

                //<MyTooltipComponent Tooltip_string_value="@data_value" />

                <div>Template : @data_value</div>

            }

          ;}

    </CascadingValue>

 

 

@code{

    [Parameter] public string Field_name { get; set; }

    [Parameter] public string Header_column_name { get; set; }

}

 


Please get back to us if you need further assistance.


Regards,

Renjith R


Attachment: ServerApp_a3325c4f.zip

Marked as answer

AP Antonio Perdomo Pastor May 30, 2022 03:47 PM UTC

Thanks, you're example helped me to solve the issue.


Best regards,

Antonio



RS Renjith Singh Rajendran Syncfusion Team May 31, 2022 05:43 AM UTC

Hi Antonio,


Thanks for your update. Please get back to us if you need further assistance.


Regards,

Renjith R


Loader.
Up arrow icon