Re: Identifying Rows
- From: Milosz Skalecki [MCAD] <mily242@xxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Dec 2007 16:31:01 -0800
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" wrote:
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).
- Follow-Ups:
- Re: Identifying Rows
- From: Mark Rae [MVP]
- Re: Identifying Rows
- References:
- Identifying Rows
- From: Ben
- RE: Identifying Rows
- From: Milosz Skalecki [MCAD]
- Re: Identifying Rows
- From: Ben
- Identifying Rows
- Prev by Date: Re: Using .NET 2.0/3.0 component in ASP.NET 1.1
- Next by Date: Re: Identifying Rows
- Previous by thread: Re: Identifying Rows
- Next by thread: Re: Identifying Rows
- Index(es):