Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9297103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:59:09+00:00 2026-06-18T21:59:09+00:00

I have a TableLayoutPanel currently with multiple rows + columns. It’s populated by a

  • 0

I have a TableLayoutPanel currently with multiple rows + columns. It’s populated by a for loop that will be different every time it’s run dependant on the output of an SQL Query.

void tblTableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        if (e.Row == 2 || e.Row == 4)
        {
            Graphics g = e.Graphics;
            Rectangle r = e.CellBounds;
            g.FillRectangle(Brushes.Red, r);
        }
    }

This is the code I’ve got currently to colour individual rows, but I’d like to be able to colour individual cells depending on 2 variables (J, and K).

                TableLayoutPanel.Controls.Add(label, J, K);

Similarly here is a sample of how I’ve added a label to a cell in the TableLayoutPanel using J and K. Is it possible to overload the CellPaint method to allow J and K to be used in it? or if so how could I go about re-colouring cells during runtime?

I’ve asked this question previously and got the following solution;

CellPaint is an event and not a method. I suggest you create a method GetColor(int row,int column) that returns a color Brush and then in the CellPaint event call your method with e.Row and e.Column.

I’m not entirely sure how this would work as the variables I am using are in the main program, wouldn’t I then need to compare these with the variables in my method? or set them somewhere to be compared and then after that do the event. Could anyone explain this a bit clearer for me? or maybe give me an example? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:59:10+00:00Added an answer on June 18, 2026 at 9:59 pm

    Here is an example:

    void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Rectangle r = e.CellBounds;
        g.FillRectangle(GetBrushFor(e.Row, e.Column), r);
    }
    
    private Brush GetBrushFor(int row, int column)
    {
        if (row == 2 && column == 1)
            return Brushes.Red;
    
        // other logic
        // ...
        // return default Brush
    }
    

    Remark – it looks strange that you want to assign color based on just on row and column index, instead of some business logic (i.e. depending on data displayed in cell).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Stack-O, I have a TableLayoutPanel with 50 rows and 6 columns. That's all well
I have a tablelayoutpanel. 2x2 - 2 columns 2 rows. For example, I added
I have a TableLayoutPanel that contains rows of labels. This control lives on form
I have a TableLayoutPanel control that is initially empty - 0 rows and 0
I'm attempting to dynamically add both columns and rows to a TableLayoutPanel. I have
I have a TableLayoutPanel and every time I put a label into one of
This is really simple. I have a TableLayoutPanel that is populated with controls (just
I have a C# .NET 3.0 project that uses a TableLayoutPanel containing several rows
I have a TableLayoutPanel starting with two columns and 0 rows. What I need
I have an autoscrolling TableLayoutPanel to which I am dynamically adding rows. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.