title: Adjust gridview rowIndex when programmatically adding new row
body: <p>I've seen this article <a href="http://asp.net-informations.com/gridview/newrow.htm" rel="nofollow">http://asp.net-informations.com/gridview/newrow.htm</a> and this post <a href="http://forums.asp.net/p/1534978/3725419.aspx#3725419" rel="nofollow">http://forums.asp.net/p/1534978/3725419.aspx#3725419</a> and I've done it for have that separator row collapsible with jquery and it's working great in display mode. The problems occuring when try to do something else with gridview, because there's a weird behaviour.. I've add a simple button that have just to set a radiobutton that's in every gridview row (except in the new added GroupHeaders rows). Then if I have added two new row he is skipping setting the last two rows in the GridView..</p>
  
  <pre><code>    public void AddNewRow(object sender, GridViewRowEventArgs e)
      {
          GridView GridView1 = (GridView)sender;
          GridViewRow NewTotalRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
          TableCell HeaderCell = new TableCell();
          HeaderCell.Attributes.Add("onclick", "collapsible('" + rowgroup + "')");
          NewTotalRow.Cells.Add(HeaderCell);
          TableCell HeaderCellIndex = new TableCell();
          int indexCorrente = e.Row.RowIndex + index;
          HeaderCellIndex.Text = indexCorrente.ToString();
          NewTotalRow.Cells.Add(HeaderCellIndex);
          GridView1.Controls[0].Controls.Add(NewTotalRow);
      }
  
      protected void gdDettaglio_RowCreated(object sender, GridViewRowEventArgs e)
      {
          bool newRow = false;
          if ((DataBinder.Eval(e.Row.DataItem, "Stato") != null))
          {
              if (statoCorrente != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Stato").ToString()))
                  newRow = true;
          }
          if (newRow)
          {
              AddNewRow(sender, e);
          }
      }
  </code></pre>
  
  <p>Printing the row index (just to check it) next to each row I'm displaying this situation (with two GroupHeader rows added):</p>
  
  <p>(The index are as they are printing them in gdDettaglio_RowCreated for GroupHeadrs rows and on gdDettaglio_OnDataBound for the other rows)</p>
  
  <pre><code>-----------------------------------------
  |           HEADER                      |
  |---------------------------------------|
  -----------------------------------------
  |           Gruppo 1        |  index -1  |
  |---------------------------------------|
  |   grp1 | x | blablabla |  |  index 0  |
  |   grp1 | y | blablabla |  |  index 1  |
  |   grp1 | z | blablabla |  |  index 2  |
  |   grp1 | x | blablabla |  |  index 3  |
  |   grp1 | x | blablabla |  |  index 4  |
  |---------------------------------------|
  |           Gruppo 2        |  index -1  |
  |---------------------------------------|
  |   grp2 | x | blablabla |  |  index 5  |
  |   grp2 | y | blablabla |  |  index 6  |
  |   grp2 | z | blablabla |  |  index 7  |
  |   grp2 | z | blablabla |  |  index 8  |
  |   grp2 | z | blablabla |  |  index 9  |
  |   grp2 | z | blablabla |  |  index 10 |
  -----------------------------------------
  </code></pre>
  
  <p>in the button code I've just:</p>
  
  <pre><code>foreach (GridViewRow riga in gdDettaglio.Rows)
  {
      if (riga.RowType == DataControlRowType.DataRow)
      {
          RadioButtonList rad = (RadioButtonList)riga.FindControl("rad");
          rad.SelectedValue = "True";
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Doing the same thing on jquery work, it affects all the row:</p>
  
  <pre><code>    function accettaTutte() {
          $("#&lt;%=gdDettaglio.ClientID%&gt; tr:has(td)").each(function () {
              var items = $(this).find("[id$='radDaPa'] input:radio'");
              for (var i = 0; i &lt; items.length; i++) {
                  if (items[i].value == 'True') {
                      if (!(items[i].checked)) {
                          items[i].checked = true;
                      }
                      break;
                  }
              }
          });
          return false;
      }
  </code></pre>
  
  <p>But I still need to do a foreach on that gridview, to update db, some idea on what could try to do? On every row I've also a "single row save" ImageButton, but clicking on it on the last two rows it's not firing the RowCommand event... It's like the two added GroupHeader rows are pushing out the last two data rows, no matter about the index.. If I click the ImageButton on the row with displayed (using Text='&lt;%#Container.DataItemIndex%>') rowIndex 2, in the rowCommand it become rowIndex 3, but it modified the right row, the one I've clicked.. If i do the same on row 7, it become 9.. But if forced it to get value on rowIndex 11, U'm getting ArgumentOutOfRangeException, because Rows.Count It's still 11..</p>