Applying styles to a GridView matching certain criteria
<p>I'm fairly new to ASP.Net so it's probably just me being a bit stupid, but I just can't figure out why this isn't working.</p>

<p>Basically, I have a GridView control (GridView1) on a page which is reading from a database. I already have a CSS style applied to the GridView and all I want to do is change the background image applied in the style depending on if a certain cell has data in it or not.</p>

<p>The way I'm trying to handle this change is updating the CSS class applied to each row through C#. I have the code below doing this:</p>

<pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;
    string s = row.Cells[7].Text;

    if (s.Length &gt; 0)
    {
        row.CssClass = "newRowBackground";
    }
    else
    {
        row.CssClass = "oldRowBackground";
    }
}
</code></pre>

<p>In theory, the data from Cell[7] will either be null or be a string (in this case, likely a person's name).</p>

<p>The problem is that when the page loads, every row in the GridView has the new style applied to it, whether it's empty or not. However, when I change it to use hard coded examples, it works fine. So for example, the below would work exactly how I want it to:</p>

<pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;
    string s = row.Cells[7].Text;

    if (s == "Smith") //Matching a name in one of the rows
    {
        row.CssClass = "newRowBackground";
    }
    else
    {
        row.CssClass = "oldRowBackground";
    }
}
</code></pre>

<p>It seems as if the top piece of code is always returning the string with a value greater than 0, but when I check the database the fields are all null (except for my test record of "Smith").</p>

<p>I'm probably doing something very simple that's wrong here, but I can't see what. Like I said, I'm still very new to this. One thing I have tried is changing the argument in the if statement to things like: <code>if (s != null)</code>, <code>if (s != "")</code> and <code>if (s == string.empty)</code> all with no luck.</p>

<p>Any help is greatly appreciated and don't hesitate to tell me if I'm just being stupid here. :)</p>
