Identifying Rows

B

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 <LABEL> 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 + "<BR>");
else
Response.Write("not found (" + gvr.Cells[1].Text +
iCount.ToString() + "<BR>");

iCount++;
}
}


Thank you!
 
M

Mark Rae [MVP]

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

Milosz Skalecki [MCAD]

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?):

<asp:GridView runat="server" ID="gv" DataKeyNames="Monitor">
</asp:GridView>

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
 
B

Ben

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?):

<asp:GridView runat="server" ID="gv" DataKeyNames="Monitor">
</asp:GridView>

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 said:
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 <LABEL> 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 + "<BR>");
                else
                    Response.Write("not found (" + gvr.Cells[1].Text +
iCount.ToString() + "<BR>");
                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
"<LABEL ID=\"MonitorID0\" Name=\"MonitorID0\">12a2b6ae-1291-2886-
c9f5-99cde6e28cd0</LABEL>Missing 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).
 
P

Phil H

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 <LABEL> 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 + "<BR>");
                else
                    Response.Write("not found (" + gvr..Cells[1].Text +
iCount.ToString() + "<BR>");

                iCount++;
            }
        }

Thank you!

Hi Ben

(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.
 
M

Mark Rae [MVP]

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?):

<asp:GridView runat="server" ID="gv" DataKeyNames="Monitor">
</asp:GridView>

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 said:
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 <LABEL> 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 + "<BR>");
else
Response.Write("not found (" + gvr.Cells[1].Text +
iCount.ToString() + "<BR>");
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
"<LABEL ID=\"MonitorID0\" Name=\"MonitorID0\">12a2b6ae-1291-2886-
c9f5-99cde6e28cd0</LABEL>Missing 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).
 
M

Mark Rae [MVP]

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

Mark Rae [MVP]

(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...
 
M

Milosz Skalecki [MCAD]

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:

<asp:GridView runat="server" ID="gv" DataKeyNames="MonitorID,AnotherColumn">
</asp:GridView>

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 said:
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?):

<asp:GridView runat="server" ID="gv" DataKeyNames="Monitor">
</asp:GridView>

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 said:
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 <LABEL> 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 + "<BR>");
else
Response.Write("not found (" + gvr.Cells[1].Text +
iCount.ToString() + "<BR>");
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
"<LABEL ID=\"MonitorID0\" Name=\"MonitorID0\">12a2b6ae-1291-2886-
c9f5-99cde6e28cd0</LABEL>Missing 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).
 
M

Mark Rae [MVP]

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
<LABEL> in one of the cells" to mean that he'd created a TemplateColumn
containing an <asp:Label> control - I have a feeling now that he didn't...
 
B

Ben

Milosz Skalecki said:
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
<LABEL> in one of the cells" to mean that he'd created a TemplateColumn
containing an <asp:Label> control - I have a feeling now that he didn't...

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 "<LABEL>" -- 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top