Filter Template access value

Hello, I need to access the value of the cell and do conditional check


    <GridColumn Field=@nameof(Model.IsOpen) HeaderText="Status" AutoFit="true">
                    <FilterItemTemplate Context="filterObj">
                        @if (filterObj is FilterItemTemplateContext filterContext)
                        {
                            string itemTemplateValue= "";
                            if (filterContext.Value)
                            {
                                itemTemplateValue = "Open";
                            }
                            else
                            {
                                itemTemplateValue = "Closed";
                            }
                            @itemTemplateValue
                        }
                    </FilterItemTemplate
                    </GridColumn>



So im getting this error "

Cannot implicitly convert type 'object' to 'bool'

Because 

filterContext.Value cannot convert to bool. Even though the actual value in the datasource is a bool.











3 Replies

MS Monisha Saravanan Syncfusion Team July 19, 2022 01:18 PM UTC

Hi Pavel,


Greetings from Syncfusion support.


Query: “ I need to access the value of the cell and do conditional check. So im getting this error "Cannot implicitly convert type 'object' to 'bool' Because filterContext.Value cannot convert to bool. Even though the actual value in the datasource is a bool.”


We have checked your query and we suspect that you are facing issue when trying to customize the filter template based on the Boolean column values. In the below code snippet we have customized the filter values inside filter template. Kindly refer the attached code snippet for your reference.


 

<SfGrid DataSource="@Orders" AllowFiltering="true" AllowPaging="true" Height="375">

         <GridColumn Field=@nameof(Order.IsBool) HeaderText="Active" DisplayAsCheckBox="true" Width="150">

             <FilterItemTemplate>

                @{

 

                    var filterContext = (context as FilterItemTemplateContext);

 

                    var itemTemplateValue = "null";

                    if (filterContext.Value.ToString().ToLower() == "true")

                    {

                        itemTemplateValue = "Open";

                    }

                    else if(filterContext.Value.ToString().ToLower() == "false")

                    {

                    itemTemplateValue = "Close";

                    }

                    @itemTemplateValue

                }

             </FilterItemTemplate>

         </GridColumn>

    </GridColumns>

</SfGrid>

 

 


Kindly get back to us if you have further queries.


Regards,

Monisha



PA Pavel July 21, 2022 03:55 PM UTC

Hello,

I tried your solution but I'm getting this error:

Dereference of a possibly null reference.
for filterContext.Value.ToString().ToLower()

EDIT:

I fixed the issue by adding it to a blank string 


value = "" + filterContext.Value;

using the 

filterContext.Value directly caused a null reference error 




NP Naveen Palanivel Syncfusion Team July 22, 2022 06:55 PM UTC

Hi Pavel,


Welcome


We are glad to hear that your query has been resolved by you.


Please get back to us if you need further assistance.


Regards,

Naveen Palanivel.


Loader.
Up arrow icon