Pesky spaces in GridView cells

Those pesky GridView cells. I was pulling data from a GridView and putting them into another variable to later be displayed in a TextBox. But the TextBox was displaying “ ” instead of an empty string, which is what was apparently shown in the GridView. Of course, the GridView actually displays “ ”, it’s just looks like nothing is there when viewed in a browser.

To get around this, I created a teeny tiny function to return an empty string if the “ ” is found:

        private string ReplaceNbspWithEmptyString(string Value)
            return Value == " " ? string.Empty : Value;

(As I always say, ladies love guys that know how to use a ternary operator.)

And then I call the function whenever I need to reference one of the cells that might be equal to”  ”:

  string newValue = ReplaceNbspWithEmptyString(row.Cells[1].Text)

A simple fix for a simple problem.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: