简体   繁体   English

在ASP.NET GridView中上下移动行

[英]Move Rows Up and Down in ASP.NET GridView

I am trying to learn to move a row with in a gridview up and down the list by pressing either an up button or down button. 我试图通过按向上按钮或向下按钮来学习在网格视图中上下移动列表。

Below is my ASP.NET. 下面是我的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>

I currently have a delete button that deletes it from the row and retains the number sequence ID. 我目前有一个删除按钮,可将其从行中删除并保留数字序列ID。

Delete Button C# 删除按钮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();
}

I am trying to do something similar but move the row up or down in the list, also retaining the number sequence ID. 我正在尝试做类似的事情,但在列表中上下移动该行,同时保留数字序列ID。

I have an understanding it will involve the indexes of the gridview and that i will need to rebind it once done. 我有一个了解,它将涉及gridview的索引,一旦完成,我将需要重新绑定它。

Can someone please guide me in the right direction with an example. 是否有人可以指导我用一个例子正确的方向。 Thank you!! 谢谢!!

Following your example, given index is the index of the first of the two rows you want to swap: 按照您的示例,给定的索引是您要交换的两行中第一行的索引:

create a new row 创建一个新行

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

populate newRow with data from the first row 用第一行的数据填充newRow

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

Remove firstRow 删除firstRow

dt.RemoveAt[index];

Now second row has rowIndex = index, so we add new row after it 现在第二行具有rowIndex = index,因此我们在其后添加新行

dt.InsertAt[newRow, index + 1];

In this way we have pulled up second row by 1 position 这样我们就把第二排拉了1个位置

    for (int i = 0; i < lstup.Items.Count; i++)
        {
            if (lstup.Items[i].Selected)
            {

                if (i > 0 && !lstup.Items[i - 1].Selected)
                {
                    ListItem bottom = lstup.Items[i];

                    lstup.Items.Remove(bottom);
                    lstup.Items.Insert(i - 1, bottom);

                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM