简体   繁体   English

运行时在TableLayoutPanel中的彩色单元带有变量?

[英]Colour cells in a TableLayoutPanel with variables during runtime?

I have a TableLayoutPanel currently with multiple rows + columns. 我有一个TableLayoutPanel当前具有多个行+列。 It's populated by a for loop that will be different every time it's run dependant on the output of an SQL Query. 它由for循环填充,每次循环运行都将有所不同,具体取决于SQL查询的输出。

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). 这是我目前为单个行着色的代码,但是我希望能够根据两个变量(J和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? 类似地,这里是一个示例,说明如何使用J和K将标签添加到TableLayoutPanel中的单元格。是否可以重载CellPaint方法以允许在其中使用J和K? or if so how could I go about re-colouring cells during runtime? 或者,如果可以的话,我该如何在运行时为单元重新着色?

CellPaint is an event and not a method. CellPaint是事件而不是方法。 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. 我建议您创建一个GetColor(int row,int column)方法 ,该方法返回颜色Brush ,然后在CellPaint事件中使用e.Row和e.Column调用您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM