Change Datagrid Cell Value on Load

G

Guest

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.
 
M

Marina

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.
 
G

Guest

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.
 
G

Guest

Thanks Phillip. Using FindControl worked!

Phillip Williams said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top