Problem with empty cell value in GridView

Discussion in 'ASP .Net' started by Bill Gower, Aug 6, 2007.

  1. Bill Gower

    Bill Gower Guest

    I have an cell which contains a field member which is filled with
    String.Empty. When I retrieve the value in the cell with
    Cells[0].Text.ToString() and try to compare it to String.Empty, I can't
    because it contains "&nbsp". I really don't want to compare the value to
    that so how can I retrieve an empty cell so that it is String.Empty.

    Bill
    Bill Gower, Aug 6, 2007
    #1
    1. Advertising

  2. "Bill Gower" <> wrote in message
    news:...

    > I have an cell which contains a field member which is filled with
    > String.Empty. When I retrieve the value in the cell with
    > Cells[0].Text.ToString() and try to compare it to String.Empty, I can't
    > because it contains "&nbsp".


    Yes - that's how empty GridView cells are rendered in HTML. A GridView is
    nothing more than an HTML table by the time ASP.NET has rendered it to the
    client browser - the rows are <tr> elements and the individual cells are
    <td> elements. Some browsers have problems displaying <td> elements which
    are completely empty, specifically with regard to borders etc, so ASP.NET
    inserts &nbsp; to prevent this.

    > I really don't want to compare the value to that


    ??? Why on earth not?

    if (Cells[0].Text.ToString() == "&nbsp;")
    {
    // do something
    }

    What could be simpler...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 6, 2007
    #2
    1. Advertising

  3. Bill Gower

    Bill Gower Guest

    Ok that's fine. I won't make it more difficult then it is. :) But now I do
    have a real problem. I am retrieving the 3rd row which happens to have no
    value in the cell and it is actually containing the value from the first row
    same cell. When the gridview is displayed it is an empty cell but when I
    retrieve it by

    GridView.Rows[index].Cells[0].Text.ToString().

    It has the value of the cell from the first row.

    What is happening here?

    Bill

    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Bill Gower" <> wrote in message
    > news:...
    >
    >> I have an cell which contains a field member which is filled with
    >> String.Empty. When I retrieve the value in the cell with
    >> Cells[0].Text.ToString() and try to compare it to String.Empty, I can't
    >> because it contains "&nbsp".

    >
    > Yes - that's how empty GridView cells are rendered in HTML. A GridView is
    > nothing more than an HTML table by the time ASP.NET has rendered it to the
    > client browser - the rows are <tr> elements and the individual cells are
    > <td> elements. Some browsers have problems displaying <td> elements which
    > are completely empty, specifically with regard to borders etc, so ASP.NET
    > inserts &nbsp; to prevent this.
    >
    >> I really don't want to compare the value to that

    >
    > ??? Why on earth not?
    >
    > if (Cells[0].Text.ToString() == "&nbsp;")
    > {
    > // do something
    > }
    >
    > What could be simpler...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Bill Gower, Aug 6, 2007
    #3
  4. "Bill Gower" <> wrote in message
    news:u0ChG%...

    > Ok that's fine. I won't make it more difficult then it is. :) But now I
    > do have a real problem. I am retrieving the 3rd row which happens to have
    > no value in the cell and it is actually containing the value from the
    > first row same cell. When the gridview is displayed it is an empty cell
    > but when I retrieve it by
    >
    > GridView.Rows[index].Cells[0].Text.ToString().
    >
    > It has the value of the cell from the first row.
    >
    > What is happening here?


    Difficult to tell without seeing your code but, if you do a View Source on
    the page in question, you will see the HTML table rendered from the GridView
    webcontrol - does that shed any light on it...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 6, 2007
    #4
  5. Bill Gower

    Bill Gower Guest

    I checked the source and rows 1 to 3 have an entry in the cell and rows 4
    to 5 have &nbsp in the cell. When I step through the code and check the
    value at the cell from row 4, it has the value from the cell in row 1.

    Bill


    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Bill Gower" <> wrote in message
    > news:u0ChG%...
    >
    >> Ok that's fine. I won't make it more difficult then it is. :) But now I
    >> do have a real problem. I am retrieving the 3rd row which happens to
    >> have no value in the cell and it is actually containing the value from
    >> the first row same cell. When the gridview is displayed it is an empty
    >> cell but when I retrieve it by
    >>
    >> GridView.Rows[index].Cells[0].Text.ToString().
    >>
    >> It has the value of the cell from the first row.
    >>
    >> What is happening here?

    >
    > Difficult to tell without seeing your code but, if you do a View Source on
    > the page in question, you will see the HTML table rendered from the
    > GridView webcontrol - does that shed any light on it...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Bill Gower, Aug 7, 2007
    #5
  6. "Bill Gower" <> wrote in message
    news:%23liK7%...

    >I checked the source and rows 1 to 3 have an entry in the cell and rows 4
    >to 5 have &nbsp in the cell. When I step through the code and check the
    >value at the cell from row 4, it has the value from the cell in row 1.


    Please show the code you're using...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon S via DotNetMonster.com

    Gridview default cell value???

    Jon S via DotNetMonster.com, Feb 24, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    6,331
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Feb 24, 2006
  2. tshad

    Empty cell problem

    tshad, Jun 13, 2005, in forum: HTML
    Replies:
    2
    Views:
    3,541
    Jukka K. Korpela
    Jun 13, 2005
  3. Bart
    Replies:
    2
    Views:
    1,494
  4. Savvoulidis Iordanis

    Empty gridview cells and checking for empty string

    Savvoulidis Iordanis, Sep 5, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    511
    Savvoulidis Iordanis
    Sep 5, 2008
  5. RJ
    Replies:
    1
    Views:
    234
    Eliyahu Goldin
    Feb 17, 2005
Loading...

Share This Page