【问题标题】:Move Rows Up and Down in ASP.NET GridView在 ASP.NET GridView 中上下移动行
【发布时间】:2017-07-07 16:50:19
【问题描述】:

我正在尝试通过按向上按钮或向下按钮来学习在网格视图中上下移动列表。

下面是我的 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 的索引,一旦完成,我需要重新绑定它。

有人可以用一个例子指导我正确的方向。谢谢!!

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

按照您的示例,给定 index 是您要交换的两行中第一行的索引:

创建一个新行

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

用第一行的数据填充 newRow

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

删除第一行

dt.RemoveAt[index];

现在第二行有rowIndex = index,所以我们在它后面添加新行

dt.InsertAt[newRow, index + 1];

通过这种方式,我们将第二行拉高了 1 个位置

【讨论】:

    【解决方案2】:
        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);
    
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      相关资源
      最近更新 更多