Change GridView row color based on condition

前端 未结 7 1027
时光说笑
时光说笑 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:28

    Alternatively, you can cast the row DataItem to a class and then add condition based on the class properties. Here is a sample that I used to convert the row to a class/model named TimetableModel, then in if statement you have access to all class fields/properties:

    protected void GridView_TimeTable_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        var tt = (TimetableModel)(e.Row.DataItem);
                         if (tt.Unpublsihed )
                           e.Row.BackColor = System.Drawing.Color.Red;
                         else
                           e.Row.BackColor = System.Drawing.Color.Green;
                    }
                }
            }
    

提交回复
热议问题