how to find a control value in a gridview

String AC;
foreach (GridViewRow gvrow in GridView1)
{
AC = gvrow.FindControl("NID").ToString() + "@meme.com";
}
Response.Write(AC);

Error with foreach:

foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.GridView' because 'System.Web.UI.WebControls.GridView' does not contain a public definition for 'GetEnumerator'

How do I work this out?

[423 byte] By [wls] at [2007-12-22]
# 1

Instead of looping on your GridView instance, try doing so on it's Rows property ala:

String AC;
foreach (GridViewRow gvrow in GridView1.Rows)
{
AC = gvrow.FindControl("NID").ToString() + "@meme.com";
}
Response.Write(AC);

BrendanGrant at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

By using the above I get the following:

Error 1 Use of unassigned local variable 'AC'

?

wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

I also manged this error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 57: foreach (GridViewRow gvrow in GridView1.Rows)
Line 58: {
Line 59: AC = gvrow.FindControl("NID").ToString() + "@meme.com"; //error here

wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

Hi!

There are a couple ways of handling this. The reason you're getting the exception is because it can't find the control "NID". When you have items in a grid view, they're going to get different ID's because there is more than one row, and you can't have more than a single instance of an ID.

One way to work around that would be to use something like:

string AC;
foreach (GridViewRow gvrow in GridView1.Rows)
{
AC = gvrow.Cells[1].Text+
"@meme.com";
}

The best place for asking ASP.NET questions is on the ASP.NET community site, and in the forums there. Check out http://www.asp.net/welcome.aspx?tabindex=1&tabid=39.

HTH,

PEte

PeteL-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Here is what I have and I received an error.

Error 1 Use of unassigned local variable 'AC'

when I use the following:

lblAllAC.Text = AC.ToString();

So I comment it out and give it another try:

Server Error in '/SummerAssociatesEmailer' Application.
--
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
Source Error:
Line 62: foreach (GridViewRow gvrow in GridView1.Rows)
Line 63: {
Line 64: AC = gvrow.Cells[1].Text + "@meme.com";

How do I work this out?

wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6

foreach (GridViewRow gvrow in GridView1.Rows)

{

AC = gvrow.Cells[0].Text + "@meme.com;";

AC += AC;

}

lblAllAC.Text = AC.ToString();

and get one@meme.com;one@meme.com;

how do I get:

one@meme.com;two@meme.com;

wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7
how would I create the code to add all values to a string?
wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8

At first glance this appears to be quite bad as you appear to want to get the text from multiple rows... and yet instead you are clearing out your AC variable with the first time (for each row), and then appending it to itself with the second... instead, try:

AC = "";

foreach (GridViewRow gvrow in GridView1.Rows)
{
AC += gvrow.Cells[0].Text + "@meme.com;";
}

lblAllAC.Text = AC.ToString();

Here we clear our AC ahead of time so it's nice and fresh and then append each row's column to it only once before displaying it at the end.

Does this work better for you?

BrendanGrant at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 9
thank you.
wls at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 10
Thank you Pete. I have been seeing examples and descriptions all over the newsgroups and various ASP sites explaining and showing how to use Findcontrol on a gridview row... and it doesn't work. You are the first to give an explanation for that.
Ansible at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified