Change ForeColor of cell in codebehind

Hi folks,

I have this xaml:
        <syncfusion:SfDataGrid x:Name="dgLevel1" Grid.Column="0" Grid.Row="1"
            AutoGenerateColumns="True"
            ColumnSizer="AutoWithLastColumnFill"
            AutoGeneratingColumn="dgLevel1_AutoGeneratingColumn"/>

An this code behind:
        private void dgLevel1_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingColumnArgs e)
        {
            if (e.Column.MappingName == "Color")
            {
                ...
            }
            else if (e.Column.MappingName == "Description")
            {
                ...
            }

        }

Please can you provide code in code behind that change cell color (not header cell) of one cell based a mapped column? I need to change "Description" mapping forecolor based "color" mapping.

Cheers


3 Replies

JG Jai Ganesh S Syncfusion Team January 8, 2018 01:04 PM UTC

Hi Josep, 
 
You can achieve your requirement to change the ForeGroundColor based on the cell value in using CellStyleSelector like below, 
 
 
<Syncfusion:SfDataGrid x:Name="SfdataGrid" 
                               AllowEditing="True" 
                               AllowSorting="True"  
                               SelectionUnit="Cell"   
                               CellStyleSelector="{StaticResource styleselector}" 
                               AutoGenerateColumns="False" 
                               ColumnSizer="Star" 
                               ItemsSource="{Binding GDCSource}" 
                               LiveDataUpdateMode="AllowDataShaping"/> 
public class CustomStyleSelector:StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
 
        var data = item as BusinessObjects; 
 
        if (data.EmployeeID <10 && ((container as GridCell).ColumnBase.GridColumn.MappingName == "EmployeeName")) 
            return App.Current.Resources["YellowCellStyle"] as Style; 
        else if (data.EmployeeName == "Peter" && ((container as GridCell).ColumnBase.GridColumn.MappingName == "EmployeeID")) 
            return App.Current.Resources["GreenCellStyle"] as Style; 
        else 
            return App.Current.Resources["RedCellStyle"] as Style; 
                 
 
        return base.SelectStyle(item, container); 
    } 
} 
 
 
 
Sample: 
 
 Regards, 
 Jai Ganesh S


JO josep January 8, 2018 03:51 PM UTC

thanks. works


JG Jai Ganesh S Syncfusion Team January 9, 2018 04:27 AM UTC

Hi Josep, 
Thank you for the update. 
Please let us know if you need further assistance on this. 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon