Referring to cells by name rather then index

  • Thread starter Thread starter Seb
  • Start date Start date
S

Seb

As of now I execute the following code when selecting a row in a
gridview. However can I somehow refer to the cells by name rather then
their index? eg. String Name = row.Cells["Name"].Text;


protected void ExercisesGridView_SelectedIndexChanged(object
sender, EventArgs e)
{
GridViewRow row = ((GridView)sender).SelectedRow;
String Name = row.Cells[2].Text;
String Description = row.Cells[3].Text;
String ExerciseType = row.Cells[4].Text;
....


best regards
 
The biggest problem i have is the order changes, cells are dropped, added.
I'd love it if there were a way to do this w/o having to create the
dictionary.

I'm using this for business objects, loading them into lists with
datareaders. Much cleaner than TableAdapters and faster!

Given i can do this for the above:
Address ADR = new Address(); // object
ADR.BActive = DR.GetBoolean(DR.GetOrdinal("bActive"));
.......
.......

instade of DR for datareader it would be GVR for GridViewRow.

Maybe the holy grail for me and others would be a strongly typed gridview,
listview, ... However, That will do more than raise a few eyebrows, it would
also kill performance. I'm using Custom Business sobjects instead of
TableAdapters/DataSets for performance and control reasons now. Could a
custom gridView "Row" object be created?

Not meaning to add fuel to a fire, I'm serious i'd like something like that
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


Mark Rae said:
As of now I execute the following code when selecting a row in a
gridview. However can I somehow refer to the cells by name rather then
their index? eg. String Name = row.Cells["Name"].Text;

Not natively. You could, I suppose, create a dictionary of the cells and
their relative number, but that seems like an unnecessary amount of work...

What problems are you having referring to the cells by their numerical
position in the row...?
 
you could change your cells to template and then use find control.
((TextBox)((GridView)sender).SelectedRow.FindControl("MyTextBox")).Text;
Ahh, i know it's wordy, but it is what i use for the very reason you asked
the question. see my other post below
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
As of now I execute the following code when selecting a row in a
gridview. However can I somehow refer to the cells by name rather then
their index? eg. String Name = row.Cells["Name"].Text;

Not natively. You could, I suppose, create a dictionary of the cells and
their relative number, but that seems like an unnecessary amount of work...

I wont bother with that...
What problems are you having referring to the cells by their numerical
position in the row...?

As WebBuilder451 points out... changing of order, adding and dropping
and so on.


Thanks for the reply.
 
you could change your cells to template and then use find control.
((TextBox)((GridView)sender).SelectedRow.FindControl("MyTextBox")).Text;
Ahh, i know it's wordy, but it is what i use for the very reason you asked
the question. see my other post below

I was just surprised that I couldn't a native way... I wont bother
with that. Thanks anyways
 
Back
Top