Change GridView row color based on condition

前端 未结 7 1020
时光说笑
时光说笑 2020-12-01 14:04

I want to change a particular row color of gridview based on some condition, I am using ASP.NET with c#. Thank you.

7条回答
  •  忘掉有多难
    2020-12-01 14:16

    This method modifies both the back color (to dark red) and the text (to white) if a specific string ("TextToMatch") occurs in one of the columns:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[8].Text.Equals("TextToMatch"))
        {
            e.Row.BackColor = System.Drawing.Color.DarkRed;
            e.Row.ForeColor = System.Drawing.Color.White;
        }
    }
    

    Or another way to write it:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[8].Text.Equals("TextToMatch"))
        {
            e.Row.Attributes.CssStyle.Value = "background-color: DarkRed; color: White";
        }
    }
    

提交回复
热议问题