e.Item.Cells(x) alternative

C

cjburkha

I have a datagrid with some bound collumns (<asp:BoundColumn)

I would like to reference these columns and their cells(data) in code.
I have found this way, and it works ok.
Dim startDateString As String = e.Item.Cells(6).Text
I don't like this --------------------------------------------^

My problem is, if I change the order of my bound columns, or decide I
want to remove one, now I have to change all thoes hard coded indexes.

Having the row(e.item) in the above example, how I can find and
refernce a particular cell without using a hardcoded index?

Any tips/links/help is appriciated.

CJB
 
F

Flinky Wisty Pomm

If you want to access a specific control rather than a cell, you can
always use FindControl("controlName");

Personally, I always create constants for columns that I'll be directly
accessing -

e.Items.Cells[idxMyColumn].Text;

you still need to change the value of those constants, but it's more
readable and you won't forget what the column means. If you're
accessing the same column in more than method, you could always
refactor the column access to a separate method and then you only have
one place to change code.

I'm not aware of any good way of accessing a specific column without
using an index; just be careful about it and you shouldn't have too
many problems.
 
C

cjburkha

Thank you, that is a good solution. Just create variables for the
index, then I only have to change it in one place.

Thanks, maybe I should have thought of that, but sometimes you get so
focused on one route, you can't see the alternative.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top