Skip to main content Skip to footer

Alternate Row Style with Hidden Rows

C1FlexGrid For Winforms has always been an easy to use grid control with the capability to manipulate Grid Styles. We can easily apply alternate row styling using Alternate property. However, it lacks the ability to maintain the alternate row styles for C1FlexGrid with hidden rows. This situation specifically arrives when C1Flexgrid rows are hidden through code or user applies filter on it. Hence, in this blog we look to overcome this limitation by handling few of the grid events. Implementation has been summarized in the following steps:-

  1. First of all load the data in the C1FlexGrid.

  2. Drop two buttons on the form wherein on click of one button we will hide the rows and on click of the another button we will apply the alternate row styling.

  3. Create a function to apply alternate row style wherein we check the visibility of the current row and style applied on the previous row. Condition checks determine the style to be applied on the current row.

  4. Then capture the C1FlexGrid’s AfterSort and AfterFilter events wherein even after sorting and filtering we will again call the function to apply alternate row styling.

The following code snippet should get the implementation going in your application:

 c1FlexGrid1.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.SingleColumn;  
 c1FlexGrid1.AllowFiltering = true;  

 // specifying style  
 C1.Win.C1FlexGrid.CellStyle cs;  
 cs = c1FlexGrid1.Styles.Add("alt");  
 cs.BackColor = Color.Red;  

 private void btnHideRows_Click(object sender, EventArgs e)  
 {  
   // hide some rows from the grid  
   for (int r = 1; r < c1FlexGrid1.Rows.Count; r += 3)  
      c1FlexGrid1.Rows[r].Visible = false;  
 }  
 private void btnApplyAltStyles_Click(object sender, EventArgs e)  
 {  
   // apply alternate row style  
    ApplyAltStyle();  
 }  
 void ApplyAltStyle()  
 {  
   // function to apply alternate row style  
   bool alt = false;  
   for (int r = 1; r < c1FlexGrid1.Rows.Count; r++)  
     {  
        if (c1FlexGrid1.Rows[r].Visible)  
           {  
             alt = !alt;  
             c1FlexGrid1.Rows[r].Style = alt ? c1FlexGrid1.Styles["alt"] : null;  
           }  
     }  
 }  
 void c1FlexGrid1_AfterSort(object sender, C1.Win.C1FlexGrid.SortColEventArgs e)  
 {  
   // apply alternate row style once sorting is done  
   ApplyAltStyle();  
 }  
 void c1FlexGrid1_AfterFilter(object sender, EventArgs e)  
 {  
   // apply alternate row style once filtering is done  
   ApplyAltStyle();  
 } 

Hunter Haaf