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.
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
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?
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.
I had to do it by hand, which is a pain.
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.
- 5 Replies
- 2 Participants
-
JD Josh Davidson
- Mar 18, 2024 03:55 PM UTC
- Apr 4, 2024 11:06 AM UTC