Change Datagrid Cell Value on Load

Discussion in 'ASP .Net' started by =?Utf-8?B?UmljaGFyZA==?=, Sep 28, 2005.

  1. I'm trying to change a value of a cell based on another before the grid is
    displayed. I'm using the ItemDataBound event like this for a simple test:

    Select Case e.Item.ItemType
    Case ListItemType.Item, ListItemType.AlternatingItem
    If e.Item.Cells(7).Text = String.Empty Then
    e.Item.Cells(7).Text = "hello"
    End If
    End Select

    The problem? e.Item.Cells(7).Text always shows that the cell is empty for
    every row in the datagrid, even though some of the records being pulled from
    the database might have a value for that cell!

    To prove it, if I comment out the above Select/Case statements, the data
    pulled from the database into the grid is displayed as it should be.

    If it helps, the cell is a template column. Am I using the wrong event? I
    tried to ItemCreated event before trying the ItemDataBound event, but that
    didn't work either.
     
    =?Utf-8?B?UmljaGFyZA==?=, Sep 28, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?UmljaGFyZA==?=

    Marina Guest

    Each cell has controls that are all part of the cell and render its
    contents. You need to set the appropriate one.

    Look at the HTML being rendered via View Source, and see what it looks like.

    "Richard" <> wrote in message
    news:...
    > I'm trying to change a value of a cell based on another before the grid is
    > displayed. I'm using the ItemDataBound event like this for a simple test:
    >
    > Select Case e.Item.ItemType
    > Case ListItemType.Item, ListItemType.AlternatingItem
    > If e.Item.Cells(7).Text = String.Empty Then
    > e.Item.Cells(7).Text = "hello"
    > End If
    > End Select
    >
    > The problem? e.Item.Cells(7).Text always shows that the cell is empty for
    > every row in the datagrid, even though some of the records being pulled
    > from
    > the database might have a value for that cell!
    >
    > To prove it, if I comment out the above Select/Case statements, the data
    > pulled from the database into the grid is displayed as it should be.
    >
    > If it helps, the cell is a template column. Am I using the wrong event? I
    > tried to ItemCreated event before trying the ItemDataBound event, but that
    > didn't work either.
     
    Marina, Sep 28, 2005
    #2
    1. Advertising

  3. Hi Richard,

    If the cell is a template column then the value you are looking for would be
    in one of the controls within the cell (i.e. look for
    e.Item.Cells(7).Controls(0).Text).

    Your code would be clearer if you were to give that control within the
    template column an ID then would be able to find it quickly using the
    e.Item.FindControl("MyControlID") and cast it the appropriate control type.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Richard" wrote:

    > I'm trying to change a value of a cell based on another before the grid is
    > displayed. I'm using the ItemDataBound event like this for a simple test:
    >
    > Select Case e.Item.ItemType
    > Case ListItemType.Item, ListItemType.AlternatingItem
    > If e.Item.Cells(7).Text = String.Empty Then
    > e.Item.Cells(7).Text = "hello"
    > End If
    > End Select
    >
    > The problem? e.Item.Cells(7).Text always shows that the cell is empty for
    > every row in the datagrid, even though some of the records being pulled from
    > the database might have a value for that cell!
    >
    > To prove it, if I comment out the above Select/Case statements, the data
    > pulled from the database into the grid is displayed as it should be.
    >
    > If it helps, the cell is a template column. Am I using the wrong event? I
    > tried to ItemCreated event before trying the ItemDataBound event, but that
    > didn't work either.
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Sep 28, 2005
    #3
  4. Thanks Phillip. Using FindControl worked!

    "Phillip Williams" wrote:

    > Hi Richard,
    >
    > If the cell is a template column then the value you are looking for would be
    > in one of the controls within the cell (i.e. look for
    > e.Item.Cells(7).Controls(0).Text).
    >
    > Your code would be clearer if you were to give that control within the
    > template column an ID then would be able to find it quickly using the
    > e.Item.FindControl("MyControlID") and cast it the appropriate control type.
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Richard" wrote:
    >
    > > I'm trying to change a value of a cell based on another before the grid is
    > > displayed. I'm using the ItemDataBound event like this for a simple test:
    > >
    > > Select Case e.Item.ItemType
    > > Case ListItemType.Item, ListItemType.AlternatingItem
    > > If e.Item.Cells(7).Text = String.Empty Then
    > > e.Item.Cells(7).Text = "hello"
    > > End If
    > > End Select
    > >
    > > The problem? e.Item.Cells(7).Text always shows that the cell is empty for
    > > every row in the datagrid, even though some of the records being pulled from
    > > the database might have a value for that cell!
    > >
    > > To prove it, if I comment out the above Select/Case statements, the data
    > > pulled from the database into the grid is displayed as it should be.
    > >
    > > If it helps, the cell is a template column. Am I using the wrong event? I
    > > tried to ItemCreated event before trying the ItemDataBound event, but that
    > > didn't work either.
     
    =?Utf-8?B?UmljaGFyZA==?=, Sep 28, 2005
    #4
    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. Franck
    Replies:
    1
    Views:
    720
    Eliyahu Goldin
    Jun 7, 2005
  2. news.rcn.com
    Replies:
    1
    Views:
    9,681
    Chris Smith
    Jul 12, 2004
  3. =?Utf-8?B?Ym9iYnk=?=

    Change the value of cell in Grid View

    =?Utf-8?B?Ym9iYnk=?=, Sep 6, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    1,081
    Mark Rae [MVP]
    Sep 6, 2007
  4. Empire City
    Replies:
    3
    Views:
    414
    Empire City
    Apr 30, 2004
  5. RJ
    Replies:
    1
    Views:
    244
    Eliyahu Goldin
    Feb 17, 2005
Loading...

Share This Page