Question

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.

Was it helpful?

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;
            }
        }
    }

OTHER TIPS

First, you need to define a GridView in markup, like this:

<asp:GridView id="GridView1" emptydatatext="No data available." runat="server" onrowdatabound="GridView1_RowDataBound" >
    <Columns>
      <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
      <asp:boundfield datafield="CompanyName" headertext="Company Name"/>
      <asp:boundfield datafield="Address" headertext="Address"/>
      <asp:boundfield datafield="City" headertext="City"/>
      <asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
      <asp:boundfield datafield="Country" headertext="Country"/>
    </Columns>
</asp:GridView>

Note: Your GridView's DataSource needs to have public property names that match the datafield values defined in your GridView.

Second, you need to implement the onrowdatabound event defined in your GridView, which points to a method named GridView1_RowDataBound, like this:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        // Put logic here to check particular cell value
        // Here is an example of changing the second cell (`Cells` collection is zero-based) to italic
        e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top