如何使用动态表更改单个单元格的边框颜色

时间:2017-03-29 04:50:17

标签: c# asp.net

我正在创建一个动态表格,我想更改特定单元格的边框颜色,例如(0,1)。我无法弄清楚如何访问特定单元格?有什么想法吗?

protected void cmdCreate_Click(object sender, EventArgs e)
{
    //clear the table
    tbl.Controls.Clear();

    int rows = Int32.Parse(txtRows.Text);
    int cols = Int32.Parse(txtCols.Text);

    for (int row = 0; row < rows; row++)
    {
        TableRow rowNew = new TableRow();

        tbl.Controls.Add(rowNew);

        tbl.Rows.AddAt(0, TableRow row);

        for (int col = 0; col < cols; col++)
        {
            //create a new tablecell object
            TableCell cellNew = new TableCell();

            cellNew.Text = "Example Cell (" + row.ToString() + "," + col.ToString() + ")";

            cellNew.BorderColor = System.Drawing.Color.Red;

            //     cellNew.Text += col.ToString() + ")";

            if (chkBorder.Checked)
            {
                cellNew.BorderStyle = BorderStyle.Inset;
                cellNew.BorderWidth = Unit.Pixel(1);
            }

            rowNew.Controls.Add(cellNew);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过选中rowcol来查看您所在的单元格。而且您还需要指定BorderStyle,而不仅仅是颜色。

if (row == 0 && col == 1)
{
    cellNew.BorderColor = System.Drawing.Color.Red;
    cellNew.BorderStyle = BorderStyle.Solid;
}