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]
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);
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 >

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

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?