Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting a Value of a Hidden Field in Gridview

Getting a Value of a Hidden Field in Gridview

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netsharepointdatabase
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    Good day On RowDataBound event i have hidden the Field called ID that will appear after the Check Box like this

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    e.Row.Cells[1].Visible = false;
    }

    Now its time to Delete the Selected Records from a Gridview and i have the Following code on the Delete Button

    foreach (GridViewRow row in GridView1.Rows)
    {
    CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
    // only add non empty staff names
    if (chkbx != null && chkbx.Checked)
    {
    List.Add(GridView1.Columns[1]);

                    Userarr.Add(GridView1.Columns\[2\]);
    
                    for(int i = 0 ; i < List.Count ; i++) 
                    {
                        int Staff\_ID  = obj.Get\_User\_Id(Convert.ToString(Userarr\[i\]),Convert.ToString(Session\["ActiveDatabase"\]));
    
                        obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(List\[i\]), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                    }
                    
    
                }
    
            }
    

    I know you will complain about Arraylist, lets look at the Solution, i will change later. Now what am doing here is: I add the Value of the second Field and the third to Different Arraylist and using for loop and calling a method that will call a Sp that will Delete the record based on those two Fields. Now my Problem is that the Field

    List.Add(GridView1.Columns[1]);

    has been set to false in the Visible Property as you have seen above. And am getting index out of bounds errors. How can i get the value on that hidden Field. Thanks

    Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

    B I 2 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      Good day On RowDataBound event i have hidden the Field called ID that will appear after the Check Box like this

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
      e.Row.Cells[1].Visible = false;
      }

      Now its time to Delete the Selected Records from a Gridview and i have the Following code on the Delete Button

      foreach (GridViewRow row in GridView1.Rows)
      {
      CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
      // only add non empty staff names
      if (chkbx != null && chkbx.Checked)
      {
      List.Add(GridView1.Columns[1]);

                      Userarr.Add(GridView1.Columns\[2\]);
      
                      for(int i = 0 ; i < List.Count ; i++) 
                      {
                          int Staff\_ID  = obj.Get\_User\_Id(Convert.ToString(Userarr\[i\]),Convert.ToString(Session\["ActiveDatabase"\]));
      
                          obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(List\[i\]), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                      }
                      
      
                  }
      
              }
      

      I know you will complain about Arraylist, lets look at the Solution, i will change later. Now what am doing here is: I add the Value of the second Field and the third to Different Arraylist and using for loop and calling a method that will call a Sp that will Delete the record based on those two Fields. Now my Problem is that the Field

      List.Add(GridView1.Columns[1]);

      has been set to false in the Visible Property as you have seen above. And am getting index out of bounds errors. How can i get the value on that hidden Field. Thanks

      Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      B Offline
      B Offline
      binarymax
      wrote on last edited by
      #2

      You can access the text property for the cell even if it is invisible. I'm not sure why you are grabbing the column object from the grid and expecting to get a value from that. I'm also not sure why you are adding to the List in a foreach and then looping through the entire list every foreach iteration? I think you might want to try something this instead:

      foreach (GridViewRow row in GridView1.Rows)
      {
      CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
      // only add non empty staff names
      if (chkbx != null && chkbx.Checked)
      {
      int Staff_ID = obj.Get_User_Id(Convert.ToString(row.Cells[2].Text),Convert.ToString(Session["ActiveDatabase"]));

                          obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(row.Cells\[1\].Text), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                  }
      
              }
      

      Hope this helps.

      V I 2 Replies Last reply
      0
      • B binarymax

        You can access the text property for the cell even if it is invisible. I'm not sure why you are grabbing the column object from the grid and expecting to get a value from that. I'm also not sure why you are adding to the List in a foreach and then looping through the entire list every foreach iteration? I think you might want to try something this instead:

        foreach (GridViewRow row in GridView1.Rows)
        {
        CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
        // only add non empty staff names
        if (chkbx != null && chkbx.Checked)
        {
        int Staff_ID = obj.Get_User_Id(Convert.ToString(row.Cells[2].Text),Convert.ToString(Session["ActiveDatabase"]));

                            obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(row.Cells\[1\].Text), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                    }
        
                }
        

        Hope this helps.

        V Offline
        V Offline
        Vimalsoft Pty Ltd
        wrote on last edited by
        #3

        Thank you very much it Helped.

        Resolved

        Thanks

        Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

        1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          Good day On RowDataBound event i have hidden the Field called ID that will appear after the Check Box like this

          protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
          e.Row.Cells[1].Visible = false;
          }

          Now its time to Delete the Selected Records from a Gridview and i have the Following code on the Delete Button

          foreach (GridViewRow row in GridView1.Rows)
          {
          CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
          // only add non empty staff names
          if (chkbx != null && chkbx.Checked)
          {
          List.Add(GridView1.Columns[1]);

                          Userarr.Add(GridView1.Columns\[2\]);
          
                          for(int i = 0 ; i < List.Count ; i++) 
                          {
                              int Staff\_ID  = obj.Get\_User\_Id(Convert.ToString(Userarr\[i\]),Convert.ToString(Session\["ActiveDatabase"\]));
          
                              obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(List\[i\]), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                          }
                          
          
                      }
          
                  }
          

          I know you will complain about Arraylist, lets look at the Solution, i will change later. Now what am doing here is: I add the Value of the second Field and the third to Different Arraylist and using for loop and calling a method that will call a Sp that will Delete the record based on those two Fields. Now my Problem is that the Field

          List.Add(GridView1.Columns[1]);

          has been set to false in the Visible Property as you have seen above. And am getting index out of bounds errors. How can i get the value on that hidden Field. Thanks

          Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

          I Offline
          I Offline
          Ibrahim Bello
          wrote on last edited by
          #4

          Hi, 1. At what point are you getting the IndexOutOfRangeException? Have you stepped through your code? 2. Instead of hiding the ID column you can decide not to bind the ID column to the gridview in the first place, by not specifying it in the gridview boundfield collections. You can set the ID column of the datasource as a Primary Key to the GridView Rows which you can use for your delete. hope this helps, Ibrahim

          1 Reply Last reply
          0
          • B binarymax

            You can access the text property for the cell even if it is invisible. I'm not sure why you are grabbing the column object from the grid and expecting to get a value from that. I'm also not sure why you are adding to the List in a foreach and then looping through the entire list every foreach iteration? I think you might want to try something this instead:

            foreach (GridViewRow row in GridView1.Rows)
            {
            CheckBox chkbx = (CheckBox)row.FindControl("myCheckBox");
            // only add non empty staff names
            if (chkbx != null && chkbx.Checked)
            {
            int Staff_ID = obj.Get_User_Id(Convert.ToString(row.Cells[2].Text),Convert.ToString(Session["ActiveDatabase"]));

                                obj.Responsibility\_Caretaker\_Remove(Convert.ToInt32(row.Cells\[1\].Text), Staff\_ID,Convert.ToString(Session\["ActiveDatabase"\]));
                        }
            
                    }
            

            Hope this helps.

            I Offline
            I Offline
            Ibrahim Bello
            wrote on last edited by
            #5

            Oh can see you've already got the solution. Regards :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups