title: How to move the rows in the GridView Up and Down?
body: <p>I have one DataTable which has four columns such as</p>
  
  <pre><code>    MileStoneID      MileStoneName       Percentage     SeqNbr
     -------------    ---------------     ------------    ------
          1               M-One               25             1
          2               M-Two               30             2
          3               M-Three             50             3
          10              M-Four              20             4
  </code></pre>
  
  <p>I bind this datatable with one GridView. Now I have two ImageButtons "imgbtnUp" and "imgbtnDown" which shows Up and Down Arrow Image.</p>
  
  <p>When I select the second row of the GridView and Clicks the Up ImageButton, then the second row should become the first row and the first row should become the second row. Like wise when I select the second row and clicks the Down ImageButton, then the second row should become the third row and third row should become the second row. Like wise I have to move the all the rows Up and Down</p>
  
  <p>How to achieve this?
  I have tried the following coding for the Up and Down ImageButtons Click Event:</p>
  
  <pre><code>protected void imgbtnUp_Click(object sender, ImageClickEventArgs e)
  {        
      if (gvMileStone.Rows.Count &gt; 0)
      {
              int index = gvMileStone.SelectedIndex;
              if (index == 0)
              {
                  ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('You cannot move the record up!')", true);
                  return;
              }
              else
              {
                  DataTable dtUp = (DataTable)ViewState["Template"];//dtUp is the DataTable I mentioned above
                  int value = Convert.ToInt32(dtUp.Rows[index]["SeqNbr"].ToString());
                  dtUp.Rows[index]["SeqNbr"] = Convert.ToInt32(index + 1) - 1;
                  dtUp.Rows[index - 1]["SeqNbr"] = value;  
                  dtUp.DefaultView.Sort = "SeqNbr";                   
                  dtUp.AcceptChanges();
                  gvMileStone.DataSource = dtUp;
                  gvMileStone.DataBind();
                  ViewState["Template"] = dtUp;
              }
     }
     else
     {
              ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('There is no rows to move!')", true);
              return;
      }
  }
  
  protected void imgbtnDown_Click(object sender, ImageClickEventArgs e)
  {        
          if (gvMileStone.Rows.Count &gt; 0)
          {
              DataTable dtDown = (DataTable)ViewState["Template"];
              int index = gvMileStone.SelectedIndex;
              if (index + 1 == dtDown.Rows.Count)
              {
                  ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('You cannot move the record down!')", true);
                  return;
              }
              else
              {
                  int value = Convert.ToInt32(dtDown.Rows[index]["MileStoneID"].ToString());
                  dtDown.Rows[index]["MileStoneID"] = Convert.ToInt32(dtDown.Rows[index]["MileStoneID"].ToString()) + 1;
                  dtDown.Rows[index + 1]["MileStoneID"] = value;
                  dtDown.AcceptChanges();
                  dtDown.DefaultView.Sort = "MileStoneID";
                  dtDown.AcceptChanges();
                  FillGrid(dtDown, gvMileStone);
                  ViewState["Template"] = dtDown;
              }
          }
          else
          {
              ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('There is no rows to move!')", true);
              return;
          }
   }
  </code></pre>
  
  <p>and My GridView Column's source code is as follows:</p>
  
  <pre><code>&lt;Columns&gt;
      &lt;asp:TemplateField Visible="false"&gt;
          &lt;ItemTemplate&gt;
             &lt;asp:Label ID="lblMileStoneID" runat="server" Text='&lt;%# Bind("MileStoneID") %&gt;'&gt;&lt;/asp:Label&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:BoundField DataField="MileStoneName" HeaderText="MileStone Name" SortExpression="MileStoneName" ItemStyle-Width="130px" /&gt;
      &lt;asp:TemplateField HeaderText="Percentage"&gt;
          &lt;ItemTemplate&gt;
             &lt;asp:TextBox ID="txtPercentage" runat="server" Text='&lt;%# Bind("Percentage") %&gt;' Width="50px" onkeypress="return DecimalValidate(event);"&gt;&lt;/asp:TextBox&gt;
          &lt;/ItemTemplate&gt;
          &lt;ItemStyle Width="100px" /&gt;
          &lt;ControlStyle Width="100px" /&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField Visible="false"&gt;
          &lt;ItemTemplate&gt;
             &lt;asp:Label ID="lblSeqNbr" runat="server" Text='&lt;%# Bind("SeqNbr") %&gt;'&gt;&lt;/asp:Label&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField Visible="false"&gt;
      &lt;ItemTemplate&gt;
         &lt;asp:LinkButton ID="lnkSelect" runat="server" CommandName="select" Text="select" /&gt;
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;                                    
  </code></pre>
  
  <p>At first, some rows are moving correctly, but after two or three click events its not working. How to move the Entire row Up and Down in the GridView? please help me.</p>