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?
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
> 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 in one of the cells
> and trying to access it via FindControl... but it's always returning
> null.
>
> I even made each LABEL have it's own ID (being equal to MonitorID1, 2,
> 3... etc), it's still not getting it.. here's a snippet, maybe someone
> has ideas on what's wrong?
>
> protected void btnSetMaintenance_Click(object sender, EventArgs e)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 int iCount =3D 0;
> =A0 =A0 =A0 =A0 =A0 =A0 foreach (GridViewRow gvr in gridIssues.Rows)
> =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Label lbl =3D
> (Label)gvr.Cells[1].FindControl("MonitorID" + iCount.ToString());
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if(lbl !=3D null)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Response.Write(lbl.Text + " ");=
(In deference to Mark) 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. If you wish to update the
data table then use a query to retreive it, modify it and apply the
updates using ADO.NET Databound controls can then be refreshed with
the Databind() method. It may well be that only an individual record
needs updating in some way, where its identity is signalled by user
selection from a grid, but in those cases it's only the primary key
that is needed.
The newly introduced data controls in ASP.NET v 2 did extend the
automatic updating from databound columns etc quite considerably, but
that only works for individual rows when in edit mode. Wider updates
have to be applied separately in the manner suggested above.
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 in one of the cells
> > and trying to access it via FindControl... but it's always returning
> > null.
>
> > I even made each LABEL have it's own ID (being equal to MonitorID1, 2,
> > 3... etc), it's still not getting it.. here's a snippet, maybe someone
> > has ideas on what's wrong?
>
> > protected void btnSetMaintenance_Click(object sender, EventArgs e)
> > {
> > int iCount = 0;
> > foreach (GridViewRow gvr in gridIssues.Rows)
> > {
> > Label lbl =
> > (Label)gvr.Cells[1].FindControl("MonitorID" + iCount.ToString());
>
> > if(lbl != null)
> > Response.Write(lbl.Text + " ");
> > else
> > Response.Write("not found (" + gvr.Cells[1].Text +
> > iCount.ToString() + " ");
>
> > iCount++;
> > }
> > }
>
> > Thank you!- Hide quoted text -
>
> - Show quoted text -
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:
The 2nd approach with the DataKeys worked for me... but would be nice
to have the first option as well (in case i want to have multiple
hidden values in a row that i can refer to).
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
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:
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 in one of the cells
> > > and trying to access it via FindControl... but it's always returning
> > > null.
> >
> > > I even made each LABEL have it's own ID (being equal to MonitorID1, 2,
> > > 3... etc), it's still not getting it.. here's a snippet, maybe someone
> > > has ideas on what's wrong?
> >
> > > protected void btnSetMaintenance_Click(object sender, EventArgs e)
> > > {
> > > int iCount = 0;
> > > foreach (GridViewRow gvr in gridIssues.Rows)
> > > {
> > > Label lbl =
> > > (Label)gvr.Cells[1].FindControl("MonitorID" + iCount.ToString());
> >
> > > if(lbl != null)
> > > Response.Write(lbl.Text + " ");
> > > else
> > > Response.Write("not found (" + gvr.Cells[1].Text +
> > > iCount.ToString() + " ");
> >
> > > iCount++;
> > > }
> > > }
> >
> > > Thank you!- Hide quoted text -
> >
> > - Show quoted text -
>
> 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:
>
> gvr.Cells[1].Controls.Count
> 0
> gvr.Cells[1].Text
> "12a2b6ae-1291-2886-
> c9f5-99cde6e28cd0Missing WMI Restart Event
> (webcom1stl.corp.amdocs.com)"
>
> The 2nd approach with the DataKeys worked for me... but would be nice
> to have the first option as well (in case i want to have multiple
> hidden values in a row that i can refer to).
>
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 in one of the cells" to mean that he'd created a TemplateColumn
containing an control - I have a feeling now that he didn't...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
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
> in one of the cells" to mean that he'd created a TemplateColumn
> containing an control - I have a feeling now that he didn't...=
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net
I'm pretty fresh to .NET (as u can probably tell :), and always liked
the control ASP gave me (I don't like using wizards to bind data etc
since i feel I lose flexibility and control)... I know .NET is pretty
flexible, I just have to learn all the tricks. The way I'm loading my
data is through a function I wrote that loads a DataTable into memory
(global var on the page), then binds a DataView to the GridView (based
on the users filters), my hopes are to avoid going to the server to
reload the DataTable with each filter change. So what I was doing is
when loading my DataTable, I updated the text of one of the columns
with a "" -- I see now it's not the right approach.
Thanks a lot for the responses and insight... looks like the
DataKeyNames can satisfy my needs for sure, and I will try messing
with actually adding controls to the cell.