Skip to main content Skip to footer

Display Different Colored Vertical and Horizontal Grid Lines in FlexGrid

Background:

You can set different colors for grid lines in FlexGrid using the Color property of CellBorder, but it will change the color of both the horizontal and vertical border lines.

To set a different color for each line, please follow the steps given below.

Steps to Complete:

  1. Set the border style to none for the grid.

    c1FlexGrid1.Styles.Normal.Border.Style = BorderStyleEnum.None;
  2. Handle the OwnerDrawCell event and draw the lines as given below:

    private void C1FlexGrid1_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
       {
        c1FlexGrid1.BeginUpdate();           
        for (int i = 1; i < c1FlexGrid1.Rows.Count; i++)
           {
              for (int j = 1; j < c1FlexGrid1.Cols.Count; j++)
                {
                 e.Graphics.DrawLine(new Pen(Color.Pink), new Point((int)c1FlexGrid1.GetCellRect(i, j).Left, (int)c1FlexGrid1.GetCellRect(i, j).Top), new Point((int)c1FlexGrid1.GetCellRect(i, j).Right, (int)c1FlexGrid1.GetCellRect(i, j).Top));
                 e.Graphics.DrawLine(new Pen(Color.Green), new Point((int)c1FlexGrid1.GetCellRect(i, j).Right, (int)c1FlexGrid1.GetCellRect(i, j).Top), new Point((int)c1FlexGrid1.GetCellRect(i, j).Right, (int)c1FlexGrid1.GetCellRect(i, j).Bottom));
                }
            }
        c1FlexGrid1.EndUpdate();
       }

Prabhat Sharma