Skip to main content Skip to footer

Dynamic Conditional Formatting in C1FlexGrid WPF

C1FlexGrid is a very flexible tool in terms of data representation. It provides a fantastic utility called CellFactory which can take data representation to a whole new level. This class lets you customize the grid at the cell level and hence can be very expedient in accomplishing certain scenarios. You may go through the documentation of the CellFactory class to know more about it. In this article, we'll see how the this class can prove to be useful when we need to format cells, based on conditions. For our little demo, we'll apply formatting to cells based on the text being displayed and the output would be something like this : C1FlexGrid with Conditional Text Formatting C1FlexGrid with Conditional Text Formatting First of all, to accomplish such a scenario, lets start by creating a class that inherits from the CellFactory class.

public class CustomCellFactory : CellFactory  
{  

}

Next, we need to override the CreateCellContent() method of the CellFactory class and set the background of the border element which surrounds the cell, based on conditions :

public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)  
{  
   base.CreateCellContent(grid, bdr, rng);  
   //format cells in second column  
   if (rng.Column == 2)  
   {  
      if (grid[rng.Row, rng.Column].ToString() == "Japan")  
      {  
         bdr.Background = new SolidColorBrush(Colors.LimeGreen);  
      }  
      else if (grid[rng.Row, rng.Column].ToString() == "India")  
      {  
         bdr.Background = new SolidColorBrush(Colors.MediumVioletRed);  
      }  
      else if (grid[rng.Row, rng.Column].ToString() == "United States")  
      {  
         bdr.Background = new SolidColorBrush(Colors.Yellow);  
      }  
      else if (grid[rng.Row, rng.Column].ToString() == "United Kingdom")  
      {  
         bdr.Background = new SolidColorBrush(Colors.Gold);  
      }  
   }  
}

Simple, isn't it! Now you may ask what's dynamic in this? The answer to this is in the gif image below : Dynamic Text Formatting Dynamic Text Formatting Whenever the data in the grid changes, the CreateCellContent() method gets fired and the formatting gets applied accordingly, even when the data is edited. You can use the CellFactory class to accomplish a lot of other tasks in a similar way, depending on your requirements. You may download the samples for detailed implementation from these links : Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus