Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Identifying Rows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 28 Dec 2007 23:16 GMT
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!
Mark Rae [MVP] - 28 Dec 2007 23:59 GMT
> 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...

Signature

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

Milosz Skalecki [MCAD] - 29 Dec 2007 19:19 GMT
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
Signature

Milosz

> 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
[quoted text clipped - 27 lines]
>
> Thank you!
Ben - 29 Dec 2007 23:23 GMT
On Dec 29, 1:19 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
> Hi Ben,
>
[quoted text clipped - 63 lines]
>
> - 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).
Mark Rae [MVP] - 30 Dec 2007 00:02 GMT
On Dec 29, 1:19 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
> Hi Ben,
>
[quoted text clipped - 65 lines]
>
> - 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).

Signature

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

Mark Rae [MVP] - 30 Dec 2007 00:06 GMT
> 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...

Signature

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

Milosz Skalecki [MCAD] - 30 Dec 2007 00:31 GMT
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

Signature

Milosz

> On Dec 29, 1:19 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
> wrote:
[quoted text clipped - 80 lines]
> 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 [MVP] - 30 Dec 2007 00:40 GMT
> 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...

Signature

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

Ben - 30 Dec 2007 13:48 GMT
> > Refering to Mark's reply, bound field is represendted by DataTableCell
> > (which does not contain any child controls) therefore in order to get the
[quoted text clipped - 8 lines]
> 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 "<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.
Phil H - 29 Dec 2007 23:48 GMT
> 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
[quoted text clipped - 27 lines]
>
> 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.
Mark Rae [MVP] - 30 Dec 2007 00:12 GMT
> (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...

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.