-1

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.

kayess
  • 3,384
  • 9
  • 28
  • 45
André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120

1 Answers1

1

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
}
Dienekes
  • 1,548
  • 1
  • 16
  • 24
  • ok, that works, but i am still very curious to find out the meaning of "6". Thanx – André Alçada Padez Dec 26 '10 at 19:08
  • 2
    The empty string is replace by   and that gives you the length 6. – gbs Dec 26 '10 at 21:20
  • 1
    ^^ Exactly. A gridview is rendered as a table and as per specs, a td cannot be empty, so   gets added automatically. I think you can avoid that by setting HTMLEncode to false for those fields, but that is not generally advisable. – Dienekes Dec 27 '10 at 04:31