Flexgrid ColumnHeader and first row have the same index?

Posted by: dirk.kuehnle on 16 March 2021, 10:26 pm EST

  • Posted 16 March 2021, 10:26 pm EST

    Hello,

    I use a WPF Flexgrid and try to set BackColor of cells.
    Therefore I use my own CellFactory-Class with overwritten ApplyCellStyles-Method:

        public class MyCellFactory : CellFactory
    {
    public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange range, Border bdr)
    {
    bdr.Background = new SolidColorBrush(Color.Coral);
    }
    }


    That works fine for the first row, but the problem is, that all ColumnHeaders are also painted with this BackColor.
    Same Problem with RowHeaders.

    Can you tell me, what i'm doing wrong?

    Best regards,
    Dirk
  • Marked as Answer

    Replied 17 March 2021, 12:08 am EST

    Hi Dirk,

    Thank you for sharing the code snippet.

    CellFactory's ApplyCellStyles method is used for applying styles to all the Cells/Headers. Therefore, if you want to change the background of cells only then you can conditionally check the CellType as follows:
    public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange rng, Border bdr)
    {
    base.ApplyCellStyles(grid, cellType, rng, bdr);

    // changes background of all cells
    if (cellType == CellType.Cell)
    {
    bdr.Background = Brushes.Yellow;
    }
    }

    Just in case if you want to change background of specific Cells/Rows/Columns then you can use the CellRange method parameter. For example, you can change the background of only the first row as follows:
    if (cellType == CellType.Cell && rng.Row == 0)
    {
    bdr.Background = Brushes.Yellow;
    }

    Best Regards,
    Kartik
  • Replied 17 March 2021, 8:22 pm EST

    Hi Kartik,

    thank you.
    Your Solution solved my Problem :-)

    Best Regards,
    Dirk
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels