2017-07-07 81 views
0

我正在嘗試學習通過按向上按鈕或向下按鈕向上或向下移動列表中的一行。在ASP.NET GridView中向上和向下移動行

下面是我的ASP.NET。

<asp:GridView ID="gridChecks" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowCreated="GridChecks_RowCreated" OnRowCommand="gridChecks_RowCommand" Style="margin: 0 auto; width: 850px; margin-top: 10px" CssClass="tableGlobalGenericStyle tableGlobalOption_AlternatingRow"> 
         <Columns> 
          <asp:BoundField DataField="RowNumber" HeaderText="Check ID" HeaderStyle-Width="80px" /> 
          <asp:TemplateField HeaderText="Check Title" HeaderStyle-Width="250px"> 
           <ItemTemplate> 
            <asp:TextBox ID="txtCheckTitle" runat="server" Width="240px"></asp:TextBox> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Effective From"> 
           <ItemTemplate> 
            <asp:textbox ID="txtDateFrom" runat="server" Width="150px"></asp:textbox> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Effective To"> 
           <ItemTemplate> 
            <asp:textbox ID="txtDateTo" runat="server" Width="150px"></asp:textbox> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Actions" HeaderStyle-Width="200px"> 
           <ItemTemplate> 
            <asp:Button ID="btnMoveUp" runat="server" CommandName="Up" CssClass="imageButton imageButton_UpArrow imageButton_Image" ToolTip="Click to add move check up the list" /> 
            <asp:Button ID="btnMoveDown" runat="server" CommandName="Down" CssClass="imageButton imageButton_DownArrow imageButton_Image" ToolTip="Click to add move check down the list" /> 
            <asp:Button ID="btnRemoveCheck" runat="server" OnClick="BtnRemove_Click" CssClass="imageButton imageButton_Remove imageButton_Image" ToolTip="Click to add remove check from the list" /> 
           </ItemTemplate> 
           <FooterStyle HorizontalAlign="Center" /> 
           <FooterTemplate> 
            <asp:Button ID="btnAddCheck" runat="server" OnClick="BtnAddCheck_Click" CssClass="imageButton imageButton_Add" Text="Add Check" ToolTip="Click to add new check to the list" /> 
           </FooterTemplate> 
          </asp:TemplateField> 
         </Columns> 
        </asp:GridView> 

我現在有一個刪除按鈕,從該行中刪除它,並保留數量順序ID。

刪除按鈕C#

protected void BtnRemove_Click(object sender, EventArgs e) 
{ 
    Button lb = (Button)sender; 
    GridViewRow gvRow = (GridViewRow)lb.NamingContainer; 
    int rowID = gvRow.RowIndex; 
    if (ViewState["CurrentTable"] != null) 
    { 
     DataTable dt = (DataTable)ViewState["CurrentTable"]; 
     if (dt.Rows.Count > 1) 
     { 
      if (gvRow.RowIndex < dt.Rows.Count - 1) 
      { 
       //Remove the Selected Row data and reset row number 
       dt.Rows.Remove(dt.Rows[rowID]); 
       ResetRowID(dt); 
      } 
     } 

     //Store the current data in ViewState for future reference 
     ViewState["CurrentTable"] = dt; 

     //Re bind the GridView for the updated data 
     gridChecks.DataSource = dt; 
     gridChecks.DataBind(); 
    } 

    //Set Previous Data on Postbacks 
    SetPreviousData(); 
} 

我試圖做同樣的事情,但在列表中向上或向下移動的行,也保留了數字序列ID。

我有一個瞭解會涉及在GridView的指標,我需要重新綁定一次。

有人可以指導我用一個例子正確的方向。謝謝!!

+0

的可能的複製[如何移動的行在GridView的上下?](https://stackoverflow.com/questions/7994371/how-to-move-the-rows-in-the-gridview-up-and-down) – AsifAli72090

+0

您將需要保存更改爲數據庫。否則,它不會堅持更改。 – Win

回答

1

按照你的例子,給指數是要交換的第一個兩行的索引:

從第一行創建一個新的行

var firstRow = dt[index]; 
var newRow = dt.NewRow(); 

填入NEWROW數據

for (int i=0; i<dt.Colums.Count(); i++) 
    newRow[i]=firstRow[i] 

刪除FIRSTROW

dt.RemoveAt[index]; 

現在,第二排具有的rowIndex =指數,所以我們增加新行後

dt.InsertAt[newRow, index + 1]; 

這樣,我們通過1個位置拉起第二排