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

referencing a TableCell in a bound datagrid

P: n/a
Is there any way to reference a cell in a datagrid's ItemDataBound event by
it's DataItem column name (e.item.DataItem("colname")). I need to set a
cell's text to "" based on the contents of it's dataitem. I have this
working now by just referencing the cell's index (e.Item.Cells.Item(4)) but
I'm afraid of that breaking in the future if the columns get rearranged. So
can I derive the cell index from the DataItem? I couldn't come up with a way
that worked.

Here's what I'm currently doing:
Dim cell As TableCell

cell = e.Item.Cells.Item(4)

'hide the default date for non-date items

Dim dtStartDate As Date = e.Item.DataItem("start_date")

If dtStartDate.ToShortDateString = "1/1/0001" Then

cell.Text = ""

End If

-------------------------------------

So I'd really love to not have to specify 4 as my item and somehow derrive
that so if later that ends up being column 3 it won't break. Any ideas?
Thanks!

Matt


Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
MattB wrote:
Is there any way to reference a cell in a datagrid's ItemDataBound event by
it's DataItem column name (e.item.DataItem("colname")).


Matt, the DataGridColumn collection can only be indexed by ordinal
value, not by a string key. Sorry.

However, if you are having the DataGrid generated with
AutoGenerateColumns=True (the default), or if the DataGrid's columns are
precisely the same as the columns in the SQL query and in the same
order, you can use this little hack as described by Patrick Philoppot:
http://datawebcontrols.com/faqs/Prog...nsByName.shtml

Happy Programming!

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
Nov 18 '05 #2

P: n/a
Use template columns, and label controlx with real IDs so you can use
FindControl. Column order won't matter, and your code will be more readable
and much more maintainable
..
"MattB" <so********@yahoo.com> wrote in message
news:c7***********@ID-86156.news.uni-berlin.de...
Is there any way to reference a cell in a datagrid's ItemDataBound event by it's DataItem column name (e.item.DataItem("colname")). I need to set a
cell's text to "" based on the contents of it's dataitem. I have this
working now by just referencing the cell's index (e.Item.Cells.Item(4)) but I'm afraid of that breaking in the future if the columns get rearranged. So can I derive the cell index from the DataItem? I couldn't come up with a way that worked.

Here's what I'm currently doing:
Dim cell As TableCell

cell = e.Item.Cells.Item(4)

'hide the default date for non-date items

Dim dtStartDate As Date = e.Item.DataItem("start_date")

If dtStartDate.ToShortDateString = "1/1/0001" Then

cell.Text = ""

End If

-------------------------------------

So I'd really love to not have to specify 4 as my item and somehow derrive
that so if later that ends up being column 3 it won't break. Any ideas?
Thanks!

Matt

Nov 18 '05 #3

P: n/a
Sounds promising. I'll check it out - thanks.

Matt

Rick Spiewak wrote:
Use template columns, and label controlx with real IDs so you can use
FindControl. Column order won't matter, and your code will be more
readable and much more maintainable
.
"MattB" <so********@yahoo.com> wrote in message
news:c7***********@ID-86156.news.uni-berlin.de...
Is there any way to reference a cell in a datagrid's ItemDataBound
event by it's DataItem column name (e.item.DataItem("colname")). I
need to set a cell's text to "" based on the contents of it's
dataitem. I have this working now by just referencing the cell's
index (e.Item.Cells.Item(4)) but I'm afraid of that breaking in the
future if the columns get rearranged. So can I derive the cell index
from the DataItem? I couldn't come up with a way that worked.

Here's what I'm currently doing:
Dim cell As TableCell

cell = e.Item.Cells.Item(4)

'hide the default date for non-date items

Dim dtStartDate As Date = e.Item.DataItem("start_date")

If dtStartDate.ToShortDateString = "1/1/0001" Then

cell.Text = ""

End If

-------------------------------------

So I'd really love to not have to specify 4 as my item and somehow
derrive that so if later that ends up being column 3 it won't break.
Any ideas? Thanks!

Matt


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.