none
how to find a control value in a gridview

    Question

  • 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?
    Friday, June 09, 2006 7:05 PM

Answers

  • 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

    Saturday, June 10, 2006 2:05 AM
  • 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?

    Tuesday, June 13, 2006 7:29 PM

All replies

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

    Friday, June 09, 2006 7:09 PM
  • By using the above I get the following:

    Error 1 Use of unassigned local variable 'AC' 

    ?

    Friday, June 09, 2006 7:23 PM
  • 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

    Friday, June 09, 2006 7:32 PM
  • 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

    Saturday, June 10, 2006 2:05 AM
  • 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?

    Monday, June 12, 2006 12:49 PM
  • 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;

     

    Monday, June 12, 2006 4:09 PM
  • how would I create the code to add all values to a string?
    Tuesday, June 13, 2006 1:18 PM
  • 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?

    Tuesday, June 13, 2006 7:29 PM
  • thank you.
    Tuesday, June 13, 2006 7:34 PM
  • 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.
    Monday, July 31, 2006 5:07 PM