Hi,
I need some assistance to create a SfDataGrid with some columns with fix datatype (name, ID, details, ...) and a generic Value column.
This is the class:
public class Parameter<T>
{
public Parameter(DbParameter parameter)
{
this.parameter = parameter;
}
private DbParameter parameter;
public string Name { get { return parameter.Name; } { set { parameter.Name= value; } }
public int ID { get { return parameter.DataType; } { set { parameter.ID= value; } }
public T Value { get { return parameter.DataType; } { set { parameter.DataType = value; } }
public string DataType => parameter.DataType;
}
using for component:
foreach(var data in databaseParameters)
{
if(data.DataType == "string")
ParameterList.Add(new Parameter<string>(data);
else if(data.DataType == "int")
ParameterList.Add(new Parameter<int>(data);
...
}
component:
<SfGrid TValue="?" DataSource="@ParameterList" AllowEditing="true" ...>
<GridEditSettings .../>
<GridColumns>
<GridColumn Field="@nameof(?)" HeaderText="ID" IsPrimaryKey="true" Visible="false"/>
<GridColumn Field="@nameof(?)" HeaderText="Name"/>
...
<GridColumn Field="@nameof(?)" HeaderText="Value">
<Template> also tested with <EditTemplate>
@{
if(context is Parameter<string> stringParameter)
{
<SfTextBox @bind-Value="@stringParameter" .../>
}
else if(context is Parameter<int> intParameter)
...
}
</Template>
</GridColumn>
</SfGrid>
Problems/Questions:
1. TValue: Its not possible to set a class with an unknown generic type. can be solved with using a base class or interface.
2. Field: Similar as 1. but cannot use a base class or interface with generics without having the same problem (1.)
3. With base class or interface and without the Field attribute of GridColumn its not working. Cannot change the values. And I read in your documentation that the Field attribute is needed to change values in the template!
4. Would be nice to be able to use GridColumn like that:
<GridColumns>
@{
if(context is Parameter<string> stringParameter)
<GridColumn Field="@nameof(stringParameter.Value)" Header="Value" Type="ColumnType.String" EditType="EditType.DefaultEdit"/>
else if(context is Parameter<int> intParameter)
<GridColumn Field="@nameof(intParameter.Value)" Header="Value" Type="ColumnType.Numeric" EditType="EditType.NumericEdit"/>
...
</GridColumns>
but the context is just available within the GridColumn node :(
Do you have some suggenstions how I can handle this? Currently I have to build a "simple" html table and add the controls accding the type in each <td /> element
Best regards,
Bernd