0

I have a htmltable. Each of the cell of the table is identified by an ID, for example 0001 and so on. The table has not a fixed dimension, but dynamic, so there can be 20 or more cells depending on how much values are stored in the db. I would like to change the backgorund color of the textbox of a specific cell. But don't know how to access the cell.

I know this syntax:

// the whole background becomes green 
myTable.BgColor = "#008000"; 
// I see no changes
myTable.Rows[x].Column[y].BgColor = "#008000";


// I need a syntax like this
myTable.Cell(Id_cell).BgColor =  "#008000";
4

2 回答 2

0

尝试这个:

.aspx:

<asp:Table ID="table" runat="server" />

C#代码:

TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "Testing";
cell.BackColor = System.Drawing.Color.Red;
row.Cells.Add(cell);
table.Rows.Add(row);

table.Rows[0].Cells[0].BackColor = System.Drawing.Color.Pink;
于 2013-02-06T10:12:37.690 回答
0

您可以像这样设置单个单元格的背景颜色:

myTable.Rows[0].Cells[1].BgColor = "#553322";

其中 0 是您想要的行号,在这种情况下,第一行和 1 是您想要的单元格编号,在这种情况下,第二个单元格作为索引基于 0。这必须在呈现表格之前完成,例如在页面加载时.

您可以将其概括为一种为 id 设置单元格颜色的方法,如下所示:

private void SetColorByCellId(HtmlTable table, string id, string color)
{
    for (int i = 0; i < table.Rows.Count; i++)
    {
        for (int j = 0; j < table.Rows[i].Cells.Count; j++)
        {
            if (table.Rows[i].Cells[j].ID == id)
            {
                table.Rows[i].Cells[j].BgColor = color;
            }
        }
    }
}

然后你会这样称呼它:

SetColorByCellId(myTable, "0001", "#553322");
于 2013-02-06T10:17:19.623 回答