c# - 如何上下移动 GridView 中的行?

标签 c# asp.net visual-studio-2008 gridview datatable

我有一个包含四列的数据表,例如

    MileStoneID      MileStoneName       Percentage     SeqNbr
   -------------    ---------------     ------------    ------
        1               M-One               25             1
        2               M-Two               30             2
        3               M-Three             50             3
        10              M-Four              20             4

我将此数据表与一个 GridView 绑定(bind)。现在我有两个 ImageButtons“imgbtnUp”和“imgbtnDown”,它们显示向上和向下箭头图像。

当我选择 GridView 的第二行并单击向上图像按钮时,第二行应成为第一行,第一行应成为第二行。同样,当我选择第二行并单击 Down ImageButton 时,第二行应该变成第三行,第三行应该变成第二行。同样明智的是,我必须上下移动所有行

如何实现? 我已经为向上和向下 ImageButtons 单击事件尝试了以下编码:

protected void imgbtnUp_Click(object sender, ImageClickEventArgs e)
{        
    if (gvMileStone.Rows.Count > 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 > 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;
        }
 }

而My GridView Column的源码如下:

<Columns>
    <asp:TemplateField Visible="false">
        <ItemTemplate>
           <asp:Label ID="lblMileStoneID" runat="server" Text='<%# Bind("MileStoneID") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="MileStoneName" HeaderText="MileStone Name" SortExpression="MileStoneName" ItemStyle-Width="130px" />
    <asp:TemplateField HeaderText="Percentage">
        <ItemTemplate>
           <asp:TextBox ID="txtPercentage" runat="server" Text='<%# Bind("Percentage") %>' Width="50px" onkeypress="return DecimalValidate(event);"></asp:TextBox>
        </ItemTemplate>
        <ItemStyle Width="100px" />
        <ControlStyle Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
        <ItemTemplate>
           <asp:Label ID="lblSeqNbr" runat="server" Text='<%# Bind("SeqNbr") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
    <ItemTemplate>
       <asp:LinkButton ID="lnkSelect" runat="server" CommandName="select" Text="select" />
    </ItemTemplate>
    </asp:TemplateField>
</Columns>                                    

起初,一些行移动正确,但在两三个点击事件后它就不起作用了。如何在 GridView 中上下移动整行?请帮助我。

最佳答案

试试这个。

您已选择存储在索引变量中的索引。
现在创建一个

DataRow row;
row = dtUp.row[index];
dtUp.Rows.RemoveAt[index];
dtUp.Rows.InsertAt[dr,index+1] //for down
dtUp.Rows.InsertAt[dr,index-1] //for up 

关于c# - 如何上下移动 GridView 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994371/

相关文章:

c# - 在 Include 语句中使用 Where 子句的 Linq 查询

asp.net - 突出显示选定的行 GridView

visual-studio-2008 - Visual Studio 2008 中缺少代码分析

database - Visual Studio 2008 中的 PostgreSQL 数据连接/服务器资源管理器

c# - 从IList + C#获取前3条记录

c# - 非泛型类中的泛型方法

asp.net - 如何在vb.net中访问RadioButtonList中的radiobutton

javascript - 将图表宽度设置为 70%

visual-studio-2008 - 如何防止 Server 2008 R2 开发箱上出现 "... has stopped working"窗口?

c# - AutoMapper 最佳实践——我应该向 DAO 询问信息以完成从 DTO 到域对象的映射吗?