Skip to content

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

Notifications You must be signed in to change notification settings

SyncfusionExamples/how-to-change-the-cell-background-based-on-the-cell-value-in-winforms-datagrid

Repository files navigation

How to Change the Cell Background Based on the Cell Value in WinForms DataGrid?

This example illustrates how to change the cell background based on the cell value in WinForms DataGrid (SfDataGrid).

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages