Skip to main content Skip to footer

Conditional Formatting in Flexgrid for LightSwitch

ComponentOne FlexGrid for LightSwitch is a LightSwitch control extension that uses the same command bar elements as the built-in Data Grid; however, it replaces the underlying Silverlight grid control with the popular ComponentOne FlexGrid control. As a drop-in replacement, the extension does everything that the built-in Data Grid does, plus conditional formatting, cell merging, tree views, export to Excel (even from the Web), filtering, printing, grouping and more. Conditional Formatting is explained in Documentation. In this blog we will discuss the simple code of conditional formatting of one column based on the values of the other in Flexgrid for LightSwitch. We will be using a simple Customer table with properties 'Name' and 'BirthDate.' We will format the 'Name' column depending on the birth year. The below code applies the required formatting depending on the year of birth: C# Code

 partial void FlexibleCustomersGrid_Created()  
 {  
      // Write your code here.  
      IContentItemProxy proxy = this.FindControl("C1FlexGrid");  
      proxy.ControlAvailable += (s, e) =>  
      {  
           var fg = e.Control as C1FlexGrid;  
           if (fg != null)  
           {  
                fg.CellFactory = new ConditionalCellFactory();  
           }  
      };  
 }  

 public class ConditionalCellFactory : CellFactory  
 {  
      public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange rng, System.Windows.Controls.Border bdr)  
      {  
           base.ApplyCellStyles(grid, cellType, rng, bdr);  
           if ((cellType == CellType.Cell) && (rng .Column==0))  
           {  
                var row = grid.Rows[rng.Row];  
                var cst = row.DataItem as Customer;  
                if (cst != null)  
                {  
                     if (cst.BirthDate.Date.Year > 1970)  
                          bdr.Background = new SolidColorBrush(Colors.Red);  
                     else  
                         bdr.Background = new SolidColorBrush(Colors.LightGray);  
                }  
           }  
      }  
 }

VB.Net Code

 Private Sub FlexibleCustomersGrid_Created()  
      ' Write your code here.  
      Dim proxy As IContentItemProxy = Me.FindControl("C1FlexGrid")  
      AddHandler proxy.ControlAvailable, AddressOf proxy_ControlAvailable  
 End Sub  

 Private Sub proxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)  
      Dim fg = CType(e.Control, C1FlexGrid)  
      If fg IsNot Nothing Then  
           fg.CellFactory = New ConditionalCellFactory()  
      End If  
 End Sub  

 Public Class ConditionalCellFactory  
      Inherits CellFactory  
      Public Overrides Sub ApplyCellStyles(grid As C1FlexGrid, cellType__1 As CellType, rng As CellRange, bdr As System.Windows.Controls.Border)  
           MyBase.ApplyCellStyles(grid, cellType__1, rng, bdr)  
           If (cellType__1 = CellType.Cell) AndAlso (rng.Column = 0) Then  
                Dim row = grid.Rows(rng.Row)  
                Dim cst = TryCast(row.DataItem, Customer)  
                If cst IsNot Nothing Then  
                     If cst.BirthDate.Value.Date.Year > 1970 Then  
                          bdr.Background = New SolidColorBrush(Colors.Red)  
                     Else  
                          bdr.Background = New SolidColorBrush(Colors.LightGray)  
                     End If  
                End If  
           End If  
      End Sub  
 End Class

MESCIUS inc.

comments powered by Disqus