By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

Change Datagrid Cell Value on Load

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <Ri*****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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.

Nov 19 '05 #2

P: n/a
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.

Nov 19 '05 #3

P: n/a
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.

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.