Get Field or Column Name In Template

HI, 

I am creating a custom template format component and want to pass to it the name of the Field or Name of the Column, without hard coding the information.

Here is a snippet of how I am calling the custom formatter:

 

<GridColumn Field="DIN0State" HeaderText="DIN0State">
    <Template Context="context">
        <LogDisplayDigitialColumn TModel="LogDisplay" LogDisplay="@((LogDisplay)context)" ColumnName="DIN0State" />
    </Template>
</GridColumn>

This works great and I have no problem with the LogDisplayDigitalColumn formatter. What I want to do is figure out how to NOT have to hard code the ColumnName="DIN0State".

I would like to be something like ColumnName="column.Field" or some other syntax that grabs th so I can use the same syntax on other columns in this grid, other grids in the project, and I can change the field names easily without having to change the hard coded string in other places.

Is this possible?

Thanks.


5 Replies

PS Prathap Senthil Syncfusion Team March 19, 2024 09:28 AM UTC

Hi Josh,

Based on your requirement, you want to pass the field name without hard coding the information as a string. So, we suggest using dynamic column generation to employ the same syntax for different columns. We have prepared a simple sample as per your requirement. Kindly refer to the code snippet and documentation for your reference.

<SfGrid DataSource="@Orders">

    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings>

    <GridColumns>

        @foreach (var prop in typeof(OrderData).GetProperties())

        {

            if (prop.Name == "OrderID")

            {

                <GridColumn Field="@prop.Name" IsPrimaryKey="true" AllowEditing="@prop.CanWrite"></GridColumn>

 

            }

            else

            {

                <GridColumn Field="@prop.Name" Width="150">

                    <Template>

                        @{

                            var data = context as OrderData;

                            <Component ColumnName="@prop.Name"></Component>

                        }

                     

                    </Template>

                </GridColumn>

 

            }

 

 

        }

    </GridColumns>

</SfGrid>

 

@code {

    public List<OrderData> Orders { get; set; }

 

    protected override void OnInitialized()

    {

        Orders = OrderData.GetAllRecords();

    }

}



Regards,
Prathap S



JD Josh Davidson March 19, 2024 05:49 PM UTC

Thanks for the reply. The solution you suggest will cause some other issues on the component. Why not just provide a way to access the column, cell, or field info in the formatter?



PS Prathap Senthil Syncfusion Team March 20, 2024 10:47 AM UTC

Based on the previous suggestion, it is possible to use the same syntax in a different column. Otherwise, we can provide a column name in the template, so that the previous approach only needs to achieve your requirement without hardcoded strings. However, you have faced a problem. Kindly share the issue details with a runnable sample; we will validate the issue and update you promptly.



JD Josh Davidson April 3, 2024 06:45 PM UTC

I had to do it by hand, which is a pain. 



PS Prathap Senthil Syncfusion Team April 4, 2024 11:06 AM UTC

We apologize for the inconvenience. The suggested approach needs to achieve your requirement without hardcoded strings, which are not feasible to meet your requirement. Thank you for your understanding.


Loader.
Up arrow icon