Re: Identifying Rows

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Dec 29, 1:19 pm, Milosz Skalecki [MCAD] <mily...@xxxxxxxxxxxxxxxxx>
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?):

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


Quantcast