Referring to cells by name rather then index

Referring to cells by name rather then index

am 22.04.2008 14:03:59 von sebastianthegreatful

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

Re: Referring to cells by name rather then index

am 22.04.2008 14:27:20 von mark

"Seb" wrote in message
news:974f05ca-931b-4630-858b-8189824ed9de@d1g2000hsg.googleg roups.com...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Referring to cells by name rather then index

am 22.04.2008 15:10:01 von 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 [MVP]" wrote:

> "Seb" wrote in message
> news:974f05ca-931b-4630-858b-8189824ed9de@d1g2000hsg.googleg roups.com...
>
> > 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...?
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
>
>

RE: Referring to cells by name rather then index

am 22.04.2008 15:16:01 von WebBuilder451

you could change your cells to template and then use find control.
((TextBox)((GridView)sender).SelectedRow.FindControl("MyText Box")).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


"Seb" wrote:

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

Re: Referring to cells by name rather then index

am 22.04.2008 17:03:16 von sebastianthegreatful

On Apr 22, 2:27 pm, "Mark Rae [MVP]" wrote:
> "Seb" wrote in message
>
> news:974f05ca-931b-4630-858b-8189824ed9de@d1g2000hsg.googleg roups.com...
>
> > 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.

Re: Referring to cells by name rather then index

am 22.04.2008 17:06:42 von sebastianthegreatful

On Apr 22, 3:16 pm, WebBuilder451
wrote:
> you could change your cells to template and then use find control.
> ((TextBox)((GridView)sender).SelectedRow.FindControl("MyText Box")).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

Re: Referring to cells by name rather then index

am 22.04.2008 18:32:19 von mark

"WebBuilder451" wrote in message
news:5D530580-6292-4919-8A8A-DC3F2B674F74@microsoft.com...

> The biggest problem i have is the order changes, cells are dropped, added.

OK.

> I'd love it if there were a way to do this w/o having to create the
> dictionary.

AFAIK, there isn't, at least, not with the existing GridView webcontrol. Of
course, there's nothing to prevent you from creating an enhanced Grid
control which maybe uses the current one as its starting point...

> Maybe the holy grail for me and others would be a strongly typed gridview,
> listview, ...

From what you're saying, you don't really need a strongly-typed GridView per
se, since you're not interested in what *type* of data exists in each of the
cells (it's just text anyway by the time it's rendered to the client
browser), but rather a way to refer to a Cell in a Cells collection by a
name rather than by its zero-based numerical position within the Cells
collection...

Which leads me to think that the Dictionary route is probably the simplest
method...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net