Referring to cells by name rather then index

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
 
W

WebBuilder451

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

WebBuilder451

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

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

Seb

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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top