referencing a TableCell in a bound datagrid

M

MattB

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
 
S

Scott Mitchell [MVP]

MattB said:
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/ProgrammaticAccess/HidingAutoGeneratedColumnsByName.shtml

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
R

Rick Spiewak

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

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top