C#更改gridview上文本框的背景颜色

时间:2016-11-14 16:34:56

标签: c# asp.net gridview textbox

我在每行的一个单元格中有一个TextBox的网格视图。我有一个按钮用于输入数据的每一行。所以我知道我在哪一行。我想出了如何设置单元格的背景颜色,而不是TextBox的背景颜色。有谁知道怎么做?

grIndex - 我在的行。

Cells[] - 是单元格所在的列。

以下是我用来设置单元格背景颜色的代码。

GridViewListComp.Rows[grIndex].Cells[5].BackColor = Color.Yellow;

提前致谢。

2 个答案:

答案 0 :(得分:0)

您必须使用FindControl并将其强制转换回TextBox才能访问它的属性。

TextBox textbox = GridView1.Rows[grIndex].Cells[5].FindControl("TextBox1") as TextBox;
textbox.BackColor = Color.Green;

或者您可以使用OnRowDataBound事件

protected void GridViewListComp_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox textbox = e.Row.FindControl("TextBox1") as TextBox;
        textbox.BackColor = Color.Green;
    }
}

答案 1 :(得分:0)

我明白了。感谢大家的帮助。

((TextBox)GridViewListComp.Rows [grIndex] .FindControl(“txtPolicy”))。BackColor = Color.Yellow;

相关问题