Identifying Rows

Identifying Rows

am 29.12.2007 00:16:31 von Ben

In C#, I have a GridView that bounds to a DataView.
When the user click a certain button, I want it to go through the
gridview rows and update some object based on an ID.

The ID should be invisible... so I put a

Re: Identifying Rows

am 29.12.2007 00:59:14 von mark

"Ben" wrote in message
news:22209261-1ce4-4b45-9aa2-4eb38f4474a3@x69g2000hsx.google groups.com...

> In C#, I have a GridView that is bound to a DataView.
> When the user clicks a certain button, I want it to go through the
> GridView rows and update some object based on an ID.

Is the button that the user clicks outside the GridView?

Is the object to be updated also outside the GridView?

> Label lbl = (Label)gvr.Cells[1].FindControl("MonitorID" +
> iCount.ToString());

Set a breakpoint on the line above and, in the Immediate window, type
gvr.Cells[1].Controls.Count and press Enter - how many controls are in that
cell?

Then, still in the Immediate window, type gvr.Cells[1].Controls[0] and press
Enter - what do you see?

Repeat for the remaining controls in that cell...


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

RE: Identifying Rows

am 29.12.2007 20:19:02 von mily242

Hi Ben,

You don't need to use hidden label as GridView has got built in mechanism
for such purpose - DataKey. Just set DataKeyNames property to identity column
(MonitorID?):




and then it's easy to get the value for correcponding row:

// i assume MontiorID column holds integer values
int monitorID;

foreach (DataKey key in gv.DataKeys)
{
monitorID = (int)key.Value;
// do something here
}

// or if you need to loop through rows
// collection as well as use datakey value

foreach (GridViewRow row in gv.Rows)
{
monitorID = (int) gv.DataKeys[row.RowIndex].Value;
}

hope it helps
--
Milosz


"Ben" wrote:

> In C#, I have a GridView that bounds to a DataView.
> When the user click a certain button, I want it to go through the
> gridview rows and update some object based on an ID.
>
> The ID should be invisible... so I put a

Re: Identifying Rows

am 30.12.2007 00:23:48 von Ben

On Dec 29, 1:19=A0pm, Milosz Skalecki [MCAD]
wrote:
> Hi Ben,
>
> You don't need to use hidden label as GridView has got built in mechanism
> for such purpose - DataKey. Just set DataKeyNames property to identity col=
umn
> (MonitorID?):
>
>
>

>
> and then it's easy to get the value for correcponding row:
>
> // i assume MontiorID column holds integer values
> int monitorID;
>
> foreach (DataKey key in gv.DataKeys)
> {
> =A0 =A0 =A0 =A0 monitorID =3D (int)key.Value;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // do something here
>
> }
>
> // or if you need to loop through rows
> // collection as well as use datakey value
>
> foreach (GridViewRow row in gv.Rows)
> {
> =A0 =A0 =A0 =A0 monitorID =3D (int) gv.DataKeys[row.RowIndex].Value;
>
> }
>
> hope it helps
> --
> Milosz
>
>
>
> "Ben" wrote:
> > In C#, I have a GridView that bounds to a DataView.
> > When the user click a certain button, I want it to go through the
> > gridview rows and update some object based on an ID.
>
> > The ID should be invisible... so I put a

Re: Identifying Rows

am 30.12.2007 00:48:01 von Phil H

On 28 Dec, 23:16, Ben wrote:
> In C#, I have a GridView that bounds to a DataView.
> When the user click a certain button, I want it to go through the
> gridview rows and update some object based on an ID.
>
> The ID should be invisible... so I put a

Re: Identifying Rows

am 30.12.2007 01:02:57 von mark

"Ben" wrote in message
news:0f6ccb94-6b8b-4886-8c25-be2a56df1ef5@z26g2000pre.google groups.com...
On Dec 29, 1:19 pm, Milosz Skalecki [MCAD]
wrote:
> Hi Ben,
>
> You don't need to use hidden label as GridView has got built in mechanism
> for such purpose - DataKey. Just set DataKeyNames property to identity
> column
> (MonitorID?):
>
>
>

>
> and then it's easy to get the value for correcponding row:
>
> // i assume MontiorID column holds integer values
> int monitorID;
>
> foreach (DataKey key in gv.DataKeys)
> {
> monitorID = (int)key.Value;
> // do something here
>
> }
>
> // or if you need to loop through rows
> // collection as well as use datakey value
>
> foreach (GridViewRow row in gv.Rows)
> {
> monitorID = (int) gv.DataKeys[row.RowIndex].Value;
>
> }
>
> hope it helps
> --
> Milosz
>
>
>
> "Ben" wrote:
> > In C#, I have a GridView that bounds to a DataView.
> > When the user click a certain button, I want it to go through the
> > gridview rows and update some object based on an ID.
>
> > The ID should be invisible... so I put a

Re: Identifying Rows

am 30.12.2007 01:06:35 von mark

"Ben" wrote in message
news:0f6ccb94-6b8b-4886-8c25-be2a56df1ef5@z26g2000pre.google groups.com...

> Thanks you very much for the responses.... the immediate window showed
> this while debugging...any ideas why it's finding 0 controls? you can
> see the Text of the cell holds the label:

Looks very much like you didn't actually add a label control to the cell in
question, but rather you added the rendered text of a label as the cell's
Text property...

That is why gvr.Cells[1].Controls.Count is returning a value of zero,
because there aren't any actual controls in the cell...


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

Re: Identifying Rows

am 30.12.2007 01:12:50 von mark

"Phil H" wrote in message
news:d114af55-949e-483e-884b-54bdb11ed8ba@t1g2000pra.googleg roups.com...

> (In deference to Mark)

There's no need at all to hold me in any sort of deference - if you think
I'm wrong, then please say so... :-)

> I've noticed that a lot of posters on this forum fail to differentiate
> properly
> between the underlying data from what is displayed in databound controls.

I understand what you're saying, but in this case it didn't seem to me that
the OP actually wanted to *change* the GridView's underlying data, but
rather to display additional metadata depending on the underlying data...


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

Re: Identifying Rows

am 30.12.2007 01:31:01 von mily242

Ben,

DataKeyNames can contain as many columns as needed, but unfortunatelly all
the values for each row are kept in ViewState which may increase overall size
of the page. Anyway:




foreach (GridViewRow row in gv.Rows)
{
DataKey dataKey = gv.DataKeys[row.RowIndex];
monitorID = (int) dataKey.Values[0];
anotherValue = (string) dataKey.Values[1];
}

Refering to Mark's reply, bound field is represendted by DataTableCell
(which does not contain any child controls) therefore in order to get the
value you must use Text property
row.Cells[index].Text

Hope this helps


--
Milosz


"Ben" wrote:

> On Dec 29, 1:19 pm, Milosz Skalecki [MCAD]
> wrote:
> > Hi Ben,
> >
> > You don't need to use hidden label as GridView has got built in mechanism
> > for such purpose - DataKey. Just set DataKeyNames property to identity column
> > (MonitorID?):
> >
> >
> >

> >
> > and then it's easy to get the value for correcponding row:
> >
> > // i assume MontiorID column holds integer values
> > int monitorID;
> >
> > foreach (DataKey key in gv.DataKeys)
> > {
> > monitorID = (int)key.Value;
> > // do something here
> >
> > }
> >
> > // or if you need to loop through rows
> > // collection as well as use datakey value
> >
> > foreach (GridViewRow row in gv.Rows)
> > {
> > monitorID = (int) gv.DataKeys[row.RowIndex].Value;
> >
> > }
> >
> > hope it helps
> > --
> > Milosz
> >
> >
> >
> > "Ben" wrote:
> > > In C#, I have a GridView that bounds to a DataView.
> > > When the user click a certain button, I want it to go through the
> > > gridview rows and update some object based on an ID.
> >
> > > The ID should be invisible... so I put a

Re: Identifying Rows

am 30.12.2007 01:40:36 von mark

"Milosz Skalecki [MCAD]" wrote in message
news:EDE6104A-03AC-45F4-A7A2-FB3FC6205413@microsoft.com...

> Refering to Mark's reply, bound field is represendted by DataTableCell
> (which does not contain any child controls) therefore in order to get the
> value you must use Text property
> row.Cells[index].Text

You are right of course, but I took the OP's statement of "so I put a

Re: Identifying Rows

am 30.12.2007 14:48:27 von Ben

On Dec 29, 6:40=A0pm, "Mark Rae [MVP]" wrote:
> "Milosz Skalecki [MCAD]" wrote in messagenews:=
EDE6104A-03AC-45F4-A7A2-FB3FC6205413@microsoft.com...
>
> > Refering to Mark's reply, bound field is represendted by DataTableCell
> > (which does not contain any child controls) therefore in order to get th=
e
> > value you must use Text property
> > row.Cells[index].Text
>
> You are right of course, but I took the OP's statement of "so I put a
>