Search code examples
asp.netvb.netrowdatabound

Selecting specific column in RowDataBound


I am trying to make it so that if there is a cell in a specific column that doesn't contain a value, i want that cell to change colour.

I don't currently have any example code that i can show but i would be grateful if anyone can help.


Solution

  • Your RowDataBound Event should like this

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "open")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                }
                else if (e.Row.Cells[0].Text == "close")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
                }
            }
        }