Search code examples
c#asp.netaspxgridview

GridView OnRowDataBound, Cell's Text.Length


My datadsource is querying the table for a varchar column, that either comes out empty or comes out something like "1,2,3,4,5".

On the RowDataBound event I want to test if the string is not empty so I can substitute that string with an image or whatever. But

e.Row.Cells[0].Text.Length  

returns 9 for the populated Cells (and this is correct), and returns 6 for the empty ones.

Can someone explain this to me? It's not just in this one column.


Solution

  • Instead, always use String.IsNullOrEmpty method to check for empty strings.

    So, in your current problem it would be:

    if String.IsNullOrEmpty(e.Row.Cells[0].Text.Trim())
    {
         // code in here would execute when the Text property is empty/null
    }