Datagrid CellStyleConverter issue

Hi all,
i'm using this CellStyleConverter with datagrid.
how is it possible that i have sometimes negative values green and positive ones red?
thanks

public class CellStyleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var amount = System.Convert.ToDouble(value);
            if (amount < 0)
                return Color.Red;
            else
                 if (amount > 0)
                return Color.Green;
            else
                return Color.Black;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

7 Replies

AN Ashok N Syncfusion Team November 23, 2017 06:58 PM UTC

Hi Alberto,  
  
Thanks for contacting Syncfusion support.  
 
You can achieve this by using the CellStyle property in the GridColumn. Please refer the below KB link to get more details about this condition style: 
  
Regards,  
Ashok   



LU Luca December 1, 2017 02:52 PM UTC

hi,
is what i'm using now but sometimes it doens't work and puts red instead of green (or viceversa)


AN Ashok N Syncfusion Team December 4, 2017 07:17 AM UTC

Hi Alberto, 
 
Thanks for your update. We have checked our SfDataGrid by customizing the GridCell using CellStyle property. We have prepared the sample by customize the GridCell for negative and non-negative values using Converter and is available for download under the following link: 
 
 
Code example:  
 
public class StyleConverter : IValueConverter 
{ 
    public StyleConverter() 
    { 
 
    } 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        double _value = (double)value; 
        if (_value < 0) 
            return Color.Red; 
        else if (_value > 0 && _value < 200) 
            return Color.Green; 
        else 
            return Color.Blue; 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        return value; 
    } 
} 
 
<sfgrid:GridNumericColumn MappingName="Freight" > 
    <sfgrid:GridTextColumn.CellStyle> 
        <Style TargetType="sfgrid:GridCell"> 
            <Setter Property="Foreground" 
                    Value="{Binding Freight,Converter={StaticResource styleConverter}}" /> 
        </Style> 
    </sfgrid:GridTextColumn.CellStyle> 
</sfgrid:GridNumericColumn> 
 
Could you please check. If still you are able to reproduce the same issue, please revert by modifying our sample based on your requirement and revert to us, otherwise please share your sample along with replication procedure, device details, Xamarin.Forms version and SfDataGrid version details that would be more helpful for further analyze. 
 
Regards, 
Ashok 



LU Luca December 4, 2017 09:13 PM UTC

thanks for your reply.
it works but not always.
i have 5 columns in a table with this converter but sometimes only 3 or 4 columns work, the last one no,
Sometimes all columns are working well...
Can you try please?


AN Ashok N Syncfusion Team December 5, 2017 01:57 PM UTC

Hi Alberto, , 
 
Thanks for sharing the detail about your converter columns details. We have checked our SfDataGrid with adding converter to five GridColumns and enable the Horizontal scrolling. Initially some converter added columns not in view and that columns comes in view while scrolling. In this time custom GridCell style not applied to that columns. We are able to reproduce the issue in this scenario. Could you please confirm whether you are facing the issue with same scenario or differ? If your replication procedure was differ from this please share the below details: 
 
  1.  SfDataGrid.ColumnSizer
  2. How many columns in View and total columns count
  3. If you have set WidthRequest and HeightRequest to SfDataGrid?
  4. Also, share Converter codes?
 
These details will be help us to proceed further. 
 
Regards, 
Ashok 



LU Luca December 15, 2017 03:36 PM UTC

gi,
i put an image

as you can see negative value are green.
there is no horizontal scroll.
how can i solve this?
regarding your question

  1.  SfDataGrid.ColumnSizer ColumnSizer="Star"
  2. How many columns in View and total columns count 9
  3. If you have set WidthRequest and HeightRequest to SfDataGrid? only HeightRequest="200"
  4. Also, share Converter codes? i try your code, it dindn't work so i'm trying this:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var amount = (decimal)value;
            if (decimal.Compare(amount, decimal.Zero) < 0)
                return Color.Red;
            else
                 if (decimal.Compare(amount, decimal.Zero) > 0)
                return Color.Green;
            else
                return Color.Black;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
thanks


AN Ashok N Syncfusion Team December 18, 2017 12:05 PM UTC

Hi Alberto,        
    
We have prepared the with all your use cases but we are not able to reproduce the reported issue without Horizontal scrolling. A support incident has been created under your account to track the status of this. Please log on to our support website to check for further updates        
        
Regards,        
Ashok       


Loader.
Up arrow icon