e.Item.Cells(x) alternative

  • Thread starter Thread starter cjburkha
  • Start date Start date
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
 
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.
 
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.
 
Back
Top