Skip to main content Skip to footer

Change BackColor of a Cell in C1Sizer

While working with C1Sizer for Winforms, at times users come across the need to set different BackColor for the individual grid-cells. Almost all grid controls have this property exposed. However, C1Sizer doesn't (its not a regular grid with regular cells either) and hence, this gets wee bit difficult. This article lets us implement the same; and this is all we need to do:

  • Subscribe to C1Sizer's Paint event.
  • Using the row and column bands of C1Sizer grid, we fetch their rectangles.
  • Find the rectangle that is their intersection (this is the cell whose BackColor we need to change).
  • Set some BackColor to the rectangle.

Here's the code:


c1Sizer1.Paint += (s1, e1) =>  
{  
 var _sizer = s1 as C1.Win.C1Sizer.C1Sizer;  

 foreach (C1.Win.C1Sizer.Band row in _sizer.Grid.Rows)  
 {  
   //Get Row rectangle  
   Rectangle row_rect = row.Bounds;  
   foreach (C1.Win.C1Sizer.Band col in _sizer.Grid.Columns)  
   {  
       //Get column rectangle  
       Rectangle col_rect = col.Bounds;  
       //Get cellractangle, which is the intersection of row\_rect & col\_rect  
       Rectangle cell\_rect = Rectangle.Intersect(row\_rect, col_rect);  
       //set BackColor  
       e1.Graphics.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(rnd.Next(255), rnd.Next(255),  
rnd.Next(255), rnd.Next(255))), cell_rect);  
       //Draw border  
       ControlPaint.DrawBorder(e1.Graphics, cell_rect, Color.Black, ButtonBorderStyle.Solid);  
    }  
 }  
};  

If you wish, you can implement additional cosmetic changes with control's border.


 c1Sizer1.Border.Thickness = new Padding(1);  
 c1Sizer1.Padding = new Padding(0);  
 c1Sizer1.SplitterWidth = 0;  
 c1Sizer1.Border.Color = Color.Black;  

And this is how C1Sizer grid looks like: Download VB.Net Sample Download C# Sample

MESCIUS inc.

comments powered by Disqus