views:

307

answers:

1

Hello Experts, Can i add particular color for entire Row or Column in TableLayoutPanel ? How ? please provide sample code if any ..

Thanks in adv.

+2  A: 

Yes you can.

Use the TableLayoutPanel's CellPaint event to test for which row/column has called the event and then use a Graphic object size to the rectangle to set the cell's color.

Like this (for the first and third rows):

     private void Form_Load(object sender, EventArgs e) {
        this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);
     }


    void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        if (e.Row == 0 || e.Row == 2) {
            Graphics g = e.Graphics;
            Rectangle r = e.CellBounds;
            g.FillRectangle(Brushes.Blue, r);
        }
    }
Jay Riggs
You want to make sure you dispose the brush. Wrap its creation in a using(){} statement or use the static Brushes.Blue. Otherwise you leak memory on every paint.
Eilon
thanks for the reminder, Eilon - and mentioning to option to use the static Brushes.
Jay Riggs