Articles in this section
Category / Section

How to change the cell background based on the cell value in WinForms DataGrid(SfDataGrid)?

1 min read

You can apply the background color for cell based on cell value can be achieve by using SfDataGrid.QueryCellStyle event.

sfDataGrid1.QueryCellStyle += SfDataGrid1_QueryCellStyle;
 
  private void SfDataGrid1_QueryCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryCellStyleEventArgs e)
{
            if (e.Column.MappingName == "Color")
            {
                var userColourString = e.DisplayText;
                int userColourNumeric = 0;
                int.TryParse(userColourString, out userColourNumeric);
                var colourToUse = userColourNumeric;
                e.Style.BackColor = ColorTranslator.FromWin32(colourToUse);
                if (e.DisplayText == "0")
                    e.Style.TextColor = Color.White;
            }
}


Background color applied based on cell value in SfDataGrid

 

View Sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied