Generic Row Entries

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



15 Replies

BP Bernd Parchmann October 7, 2020 05:16 PM UTC

Update:

Tested with Interface plus Interface<T> like that:
interface IParameter<T> : IParameter
{
new T Value {get;set;}
}
interface IParameter
{
object Value {get;set;}
}
class Parameter<T> : IParameter<T>
{
public T Value {get;set;}
public object IParameter.Value {get;set;}
}

Now the SfGrid is showing correctly my 3 test entries with type Parameter<string>. But just the last entry is chanable.


RS Renjith Singh Rajendran Syncfusion Team October 9, 2020 12:57 PM UTC

Hi Bernd, 

Greetings from Syncfusion support. 

We are not clear about your exact requirement. We suspect that you are facing difficulties in binding complex data column in Grid. If so then we suggest you to refer the below documentation for more details regarding this. 

We also suggest you to refer the below documentation for more details regarding dynamic column building from the Grid’s model class.  

If we have misunderstood your query or if you still need further assistance regarding this, then kindly share with us the following details for better assistance. 
  1. Share the sample which you have tried from your side.
  2. Share the video demo showing the problem/difficulties you are facing.
  3. Share the detailed explanation of the problem you are facing in rendering Grid and defining Grid columns.
  4. Share your complete model class codes and Grid rendering codes.

The provided information will help us analyze the problem, and provide you a solution as early as possible. 

Regards, 
Renjith Singh Rajendran 



BP Bernd Parchmann October 9, 2020 01:25 PM UTC

Shortly:
I want to have a SfGrid with generic classes like Parameter<T>. This class has some properties for name, id, ... with known datatypes. But one column is for the generic property T; my Value property/column: public T Value {get;set;}

If I want to implement this I am facing multiple problems (see first post).
Your documentation (already read and used!) has no examples for this requirement so I have used the Column Template feature to create the controls in the cells of the Value column depending of the generic data type.

Tried implementation like post 2 (interface and interface<T>) and another implementation with base class for grid datatype (and datasource).

Each implementation has other problems!
Interface: Cast exception
BaseClass: ambiguousmatchexception


RS Renjith Singh Rajendran Syncfusion Team October 14, 2020 01:47 PM UTC

Hi Bernd, 

Thanks for your update. 

We have prepared a sample based on this scenario, by having a Generic typed class as model class of Grid. Please download the sample from the link below, 
 
 
        <GridColumn Field=@nameof(Order<string>.Value) HeaderText="Value" Width="120"> 
            <Template> 
                    @{ 
                        var a = context as Order<string>; 
                        if (a.Value.GetType().Equals((typeof(string)))) 
                        { 
                            <SfTextBox @bind-Value="@a.Value"/> 
                        } 
                    } 
            </Template> 
        </GridColumn> 
 
 
We suggest you to refer the above attached sample, and try as like the same in your side. And please get back to us if you need further assistance. 

Regards, 
Renjith R 



BP Bernd Parchmann October 14, 2020 02:26 PM UTC

Hi,

I guess there is a mistake in your example.

The type of the Grid and the DataSource is Order<string>! So I am just able to add objects of type Order<string>. But I want to be able to add Order<int>, Order<bool>, ...


RS Renjith Singh Rajendran Syncfusion Team October 16, 2020 12:22 PM UTC

Hi Bernd, 

Thanks for your update. 

On analyzing your codes from first update, we suspect that, your requirement is to set the Type and EditType for the Value columns dynamically based on type(string/int) of the Value field.  

If so, then we would like to inform you that, the Type and EditType for a particular column will be handled in-built based on the value in the particular column. So it is not needed to manually defined the Type and EditType for the Value column. 

If you are still facing difficulties, then kindly share with us the following details for better assistance. 
  1. Are you facing difficulties in binding data to Grid initially?
  2. Or if you are facing difficulties in performing any other Grid operations then share those details.
  3. Share with detailed description of the problem you are facing.
  4. Share a video demo explaining the problem you are facing in rendering Grid.
  5. Share the sample which you have tried from you side.

The provided information will help us analyze the problem, and provide you a solution as early as possible. 

Regards, 
Renjith R 



BP Bernd Parchmann October 16, 2020 01:35 PM UTC

Hello again,

it's a little bit frustrating. Here is my class and component implementation and the resulting error log.



Attachment: generic_a143b318.zip


RS Renjith Singh Rajendran Syncfusion Team October 19, 2020 01:34 PM UTC

Hi Bernd, 

Thanks for sharing the details. 

We have analyzed your shared codes, and we would like to inform you that it is not possible with your model. It is suggested to use column wise typed model in Grid, instead of row wise type as like in your model. 

All the row values of a Column must be in same type to bind a column value in grid. So we suggest you to ensure to use same typed row values for that particular column. 

 
protected override async Task OnInitializedAsync() 
{ 
    ... 
    ParameterList.Add(new Parameter<string>(p1)); 
    ParameterList.Add(new Parameter<string>(p2)); 
    ParameterList.Add(new Parameter<string>(p3)); 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BP Bernd Parchmann October 20, 2020 08:13 AM UTC

Hi,

thx for the clarification but it's too bad to hear that :(

Is the problem the serialization of the generic value property?
Because then what is the template column for if I cannot react on all "code possibilities"?

Can I add another component as template column with controls to handle multiple datatypes? Or I react on the datatype property and create the controls in the template according the type (string typed information)?


RS Renjith Singh Rajendran Syncfusion Team October 23, 2020 11:45 AM UTC

Hi Bernd, 

Based on this scenario, we suggest you to use the Row Template feature of Grid to display the contents in Grid. Please refer the below documentation for more details, 

We have also prepared a sample based on your shared codes. Please download the sample form the link below, 
 
Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



FA Fausto October 28, 2020 10:58 AM UTC

I'm sorry to intrude. 

If I understand correctly, Bernd needs a component with a dynamic generic grid, which can be parameterised by any data source. 

I had a similar situation and resolved it with "@typeparam"; in my situation all data sources had the same structure (Id: int (pk), Description:string). 

Hope it helps. Once again my apologies for the intrusion... if it was not the intended, then I misunderstood; might be if you take advantage of something _-)

I've included a sample attached.

Fausto.

Attachment: GenericGrid_3021b93e.rar


RS Renjith Singh Rajendran Syncfusion Team October 29, 2020 02:08 PM UTC

Hi Fausto, 

Thanks for the suggestion. 

We could see that you are containing two column fields(ValuePropertyName, TextPropertyName) of int and string type for the corresponding columns. But Bernd’s case will be having different typed values for the single column itself (i.e.) different typed values(int, string, bool) for TextPropertyName field instead of having string typed values alone.  

So as these row wise typed models are not supported, we have suggested the RowTemplate feature for Bernd at these sceanrio. It is suggested to use column wise typed model(as like your model) in Grid, instead of row wise type model. All the row values of a Column must be in same type to bind a column value in grid.  

Regards, 
Renjith Singh Rajendran 



FA Fausto October 30, 2020 09:02 PM UTC

I didn't really understand Bernd's situation, it was the case.

Thanks for the clarification.

Regards,
Fausto


BP Bernd Parchmann November 2, 2020 07:16 AM UTC

Hi Fausto, Hi Renjith,

@Renjith: Thx for your example! With your hint (Row Template) I was able to implement my "generic datagrid"!

@Fausto: The problem is, that the SfGrid is able to cast from context to the generic object WITH DIFFERENT DATA VALUES (and Column Template!). If the generic type is the same in the datasource, it is possible to do that the way I need... But I want to have a "mixed" value column with different typed values (the generic T value). Your suggenstion with @typeparam I already tested, too. Not in the component where the Grid is (because then I have to define the generic type in the parent component!). I tested the typeparam in an generic component to show the value according the type. But the same problem: The datagrid cannot cast from context to the (not fixed) generic type in the Column Template.

But as written, with Row Template it is possible! A little less elegant as Column Template, but its working fine!

Thx, and best regards,
Bernd!


RS Renjith Singh Rajendran Syncfusion Team November 2, 2020 08:50 AM UTC

Hi Bernd, 

Thanks for your update. 

We are glad to hear that the provided suggestion helped you in achieving this requirement. 

Regards, 
Renjith R 


Loader.
Up arrow icon