473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

referencing a TableCell in a bound datagrid

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
3 2229
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron Ackerman | last post by:
I am using typed datasets in an N-Tier Windows app using VB.NET. I know this posting cannot be fully explained in a single post that is why I am asking for someone to point me to a real world...
3
by: Rajeev | last post by:
Hi I can use the TableRow and TableCell class in C# while using Web forms. The same are not available while programming with Windows Forms. I have the folloowing code that works fine with Web...
3
by: Andre | last post by:
Hi, Did someone know why this code does'nt work (The TableCell Width). The cell don't have 600 and 150 like writed in width="600" and width="150". <asp:datagrid id="DataGrid1" ...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
3
by: 2obvious | last post by:
I have a DataGrid containing a TextBox control and a CustomValidator in each row. The CustomValidator fires a function that compares all TextBoxes for equality. The algorithm for comparison is...
3
by: Lyners | last post by:
I have a table within a cell of a datagrid. I am doing updates without postback to the server using Javascript. I have everything working, except referencing a table within the datagrid cell. ...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
0
by: TonyJ | last post by:
Hello! I'm unsure when I can use a bound datagrid and when I can't. What limitations has a bound datagrid? 1. For example if I want to manipulate the data in the datasource before displaying...
0
by: DanG | last post by:
During grid field validation, I would like to generate a message for the user to tell which column had the error. So I have a control (textbox, dropdownlist, checkbox, etc.), and would like to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.