Is there a function to convert Type in Columntype ?

Hi,
I'm making a generic SfGrid component based on a class passed as a parameter.
I encountered the following problem : Filtering not working when cell is null
Therefore, I need to add the "Type" in "GridColumn".

Is there a function to convert a type Type to a type ColumnType ? Or should I do one myself ?

Best regards.
Francois

7 Replies 1 reply marked as answer

FR Francois July 30, 2020 01:13 PM UTC

I want to achieve something like this :
@typeparam T
<SfGrid TValue="T"...>
    <GridColumns>
        @foreach (var prop in typeof(T).GetProperties())
        {
            <GridColumn Field="@prop.Name"
                        Type="@function(prop.PropertyType)"
                        ...></GridColumn>
        }
    </GridColumns>
</SfGrid>


VN Vignesh Natarajan Syncfusion Team August 3, 2020 09:24 AM UTC

Hi Francois,  
 
Greetings from Syncfusion support.  
 
Query: “Is there a function to convert a type Type to a type ColumnType ? Or should I do one myself ? 
 
We have analyzed your query and we do not have public method to determine the ColumnType explicitly in Grid component. If the ColumnType is not defined, then we would determine the ColumnType internally based on the TValue of the grid.  So kindly skip defining the ColumnType if you are not aware of the type explicitly, Grid will handle this for you. 
 
Kindly get back to us if you have further queries.   
 
Regards, 
Vignesh Natarajan  
 



FR Francois August 13, 2020 09:12 AM UTC

Thank Vignesh.

You said : "So kindly skip defining the ColumnType if you are not aware of the type explicitly, Grid will handle this for you."

I can’t let the grid define types for me because I have columns filled with “null” values (at least on the first launch of my application) and I encounter this problem Filtering not working when cell is null.


I do my own method :

[code]

public ColumnType GetColumnType(Type type)
{
    if ((type == typeof(int)) || (type == typeof(int?))
            || (type == typeof(short)) || (type == typeof(short?))
            || (type == typeof(long)) || (type == typeof(long?))
            || (type == typeof(float)) || (type == typeof(float?))
            || (type == typeof(decimal)) || (type == typeof(decimal?))
            || (type == typeof(byte)) || (type == typeof(byte?)))
    {
        return ColumnType.Number;
    }
    else if ((type == typeof(bool)) || (type == typeof(bool?)))
    {
        return ColumnType.Boolean;
    }
    else if ((type == typeof(DateTime)) || (type == typeof(DateTime?))
        || (type == typeof(DateTimeOffset)) || (type == typeof(DateTimeOffset?)))
    {
        return ColumnType.DateTime;
    }
    else
    {
        return ColumnType.String;
    }
}

[/code]

François



FR Francois August 13, 2020 09:16 AM UTC

How can I post code on the forum ? I can't find the right syntax.
<code>
public ColumnType GetColumnType(Type type)
</code>


VN Vignesh Natarajan Syncfusion Team August 14, 2020 07:45 AM UTC

Hi Francois,  
 
Thanks for the update.  
 
Query: “I do my own method : 
 
We are glad to hear that you have resolved your query on your own. We have included the some of the numeric types in below code example. 
 
public ColumnType GetColumnType(Type type) 
   { 
       if ((type == typeof(int)) || (type == typeof(int?)) 
               || (type == typeof(uint)) || (type == typeof(uint?)) 
               || (type == typeof(short)) || (type == typeof(short?)) 
               || (type == typeof(ushort)) || (type == typeof(ushort?)) 
               || (type == typeof(long)) || (type == typeof(long?)) 
               || (type == typeof(ulong)) || (type == typeof(ulong?)) 
               || (type == typeof(float)) || (type == typeof(float?)) 
               || (type == typeof(decimal)) || (type == typeof(decimal?)) 
               || (type == typeof(double)) || (type == typeof(double?)) 
               || (type == typeof(byte)) || (type == typeof(byte?)) ) 
       { 
           return ColumnType.Number; 
       } 
       else if ((type == typeof(bool)) || (type == typeof(bool?))) 
       { 
           return ColumnType.Boolean; 
       } 
       else if ((type == typeof(DateTime)) || (type == typeof(DateTime?)) 
           || (type == typeof(DateTimeOffset)) || (type == typeof(DateTimeOffset?))) 
       { 
           return ColumnType.DateTime; 
       } 
       else 
       { 
           return ColumnType.String; 
       } 
   } 
 
 
Kindly get back to us if you have further queries.  
 
Query: “How can I post code on the forum ? I can't find the right syntax. 
 
We currently don’t have the support for code snippet option in the forums RTE. We have already logged a feature request for the same and will rollout this changes in our upcoming website release. As a workaround, you can copy the code from your IDE to your outlook mail and paste that content from the outlook mail to the RTE to give the code snippet as formatted, refer the screenshot below.   
  
    
 
 
Regards,
Vignesh Natarajan
 


Marked as answer

FR Francois August 15, 2020 07:59 AM UTC

Thanks Vignesh.
I will update my code and use your advice if I need to post code again.


VN Vignesh Natarajan Syncfusion Team August 17, 2020 04:58 AM UTC

Hi Francois,  

Thanks for the update. 

Kindly get back to us if you have further queries.  

Regards,
Vignesh Natarajan 


Loader.
Up arrow icon