gridview 数据绑定后,我加了两个模板列LinkButton,"上移"和"下移" 我现在已经搞定了RowCreated 和 RowCommand 
当点击某行的linkbutton时,我可以知道当前的行号我有一个按钮,是循环gridview 按表格里的顺序 更新库里的排序号我想点击"上移"或"下移"时,让这两行的内容交换位置,这个如何实现?
有好的方法吗?

解决方案 »

  1.   

    可不可以把所以的单元格的值都存起来先
    然后把这行删了,然后再在另一行的上面或者下面插入一行
    然后在把这些值放回去以前用vb6写winform 的程序的时候,是这样写的,但是asp.net就不会了
      

  2.   

    HTML页面两个BTN
    <asp:LinkButton ID="uplinkbtn" CommandName="up" CommandArgument='<%# Eval("subjectid") %>' OnClientClick='return confirm("确定上移吗?")' runat="server">向上移动</asp:LinkButton>
    <asp:LinkButton ID="downlinkbtn" CommandName="down" CommandArgument='<%# Eval("subjectid") %>' OnClientClick='return confirm("确定下移吗?")' runat="server">向下移动</asp:LinkButton>
    ======ItemCommend事件里面.cs代码
    private static LinkButton linkbtn, linkbtn1;
       /// <summary>
        /// RP命令事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void DLBlogSubjectList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Repeater repeater;
            switch (e.CommandName)
            {
                case "del":
                    linkbtn = (LinkButton)e.Item.FindControl("dellinkbtn");
                    if (BLL.BSubject.DelSubject(Convert.ToInt32(linkbtn.CommandArgument)))
                    {
                        Common.JScript.Alert("删除成功!");
                    }
                    else
                    {
                        Common.JScript.Alert("删除失败!");
                    }
                    iniDataList(this.getFormParm("sqlWhere"));
                    break;
                case "modify":
                    swin2.Visible = true;
                    linkbtn = (LinkButton)e.Item.FindControl("modifylinkbtn");
                    break;
                case "up":
                    linkbtn = (LinkButton)e.Item.FindControl("uplinkbtn");
                    repeater = (Repeater)source;
                    if (e.Item.ItemIndex > 0)
                    {
                        linkbtn1 = (LinkButton)repeater.Items[e.Item.ItemIndex - 1].FindControl("uplinkbtn");
                        if (BLL.BSubject.UpSubjectOrderNum(Convert.ToInt32(linkbtn.CommandArgument), Convert.ToInt32(linkbtn1.CommandArgument)))
                        {
                            Common.JScript.Alert("向上移动成功!");
                        }
                        else
                        {
                            Common.JScript.Alert("向上移动失败!");
                        }
                        iniDataList(this.getFormParm("sqlWhere"));
                    }
                    else
                    {
                        Common.JScript.Alert("已经是最上面一级了!");
                    }
                    break;
                case "down":
                    linkbtn = (LinkButton)e.Item.FindControl("downlinkbtn");
                    repeater = (Repeater)source;
                    if (e.Item.ItemIndex + 1 < repeater.Items.Count)
                    {
                        linkbtn1 = (LinkButton)repeater.Items[e.Item.ItemIndex + 1].FindControl("uplinkbtn");
                        if (BLL.BSubject.UpSubjectOrderNum(Convert.ToInt32(linkbtn.CommandArgument), Convert.ToInt32(linkbtn1.CommandArgument)))
                        {
                            Common.JScript.Alert("向下移动成功!");
                        }
                        else
                        {
                            Common.JScript.Alert("向下移动失败!");
                        }
                        iniDataList(this.getFormParm("sqlWhere"));
                    }
                    else
                    {
                        Common.JScript.Alert("已经是最下面一级了!");
                    }
                    break;
            }
        }
      

  3.   

    我用的REPEATER GRIDEVIEW应该也一样。。
      

  4.   

    BLL.BSubject.UpSubjectOrderNum  是不是你们自己的写的东东啊?
    移动行的代码是不是在那里面写的呢?
      

  5.   

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Control cmdSource = e.CommandSource as Control;
            GridViewRow row = cmdSource.NamingContainer as GridViewRow;
            int rowIndex = row.RowIndex;
            int id=Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);//得到本行ID
            //要得到上一行的,就拿rowindex-1,下一行的就rowindex+1
        }
      

  6.   

    你再在数据库中增加一个字段sortID  把每个记录的行号都设置一下 然后按升序的方式绑定在页面上然后你用上面我给你的方法  当移动的时候 分别更新你移动记录数据库中sortID的值
      

  7.   

    最好是用js,不然用服务器代码的话对服务器影响较大且性能不太好。等待js高手
      

  8.   


    直接操作内存中的dataset就可以,点击  上  下  按钮,移动内存中的dataset对应行的位置,知道点击"保存",再循环dataset ,保存到数据库中
      

  9.   

    如何超过内存中的dataset 然后保存?弱弱的问....
      

  10.   


    DataSet就是个DataTable的容器,你直接操作里面的DataTable  比如删除行, Remove  表克隆      Clone 添加行      AddRow......
    以前我写的一个 上下移动ListBox内条目的代码,是内存操作,确认后,才提交
    不过是vb的.呵呵 #Region " 移动右侧元素"
        '_up向上移,如果是false,则向下
        Private Sub MoveUp(ByVal _up As Boolean)
            Try
                If Me.lbox_have.SelectedIndex = -1 Then
                    Exit Sub
                End If            '记录选择是第几行,方便最后再设置选择
                Dim index As Integer
                index = Me.lbox_have.SelectedIndex
                '读取右侧选择的数据  并移出            Dim dt_Have As DataTable
                dt_Have = CType(ViewState("HaveDataTable"), DataTable)
                ViewState("HaveDataTable") = Nothing            '定义临时表
                Dim dt_temp As DataTable
                '克隆表结构
                dt_temp = dt_Have.Clone()
                '清除数据
                dt_temp.Clear()            '循环复制
                Dim i As Integer
                For i = 0 To dt_Have.Rows.Count - 1
                    If _up = True Then
                        '如果是向上运动
                        If CType(dt_Have.Rows(i)("CTID"), String) = Me.lbox_have.SelectedValue.ToString() And i <> 0 Then
                            '如果是选中行,则将当前datatable最后一个元素与它作交换,
                            dt_temp.Rows.Remove(dt_temp.Rows(i - 1))
                            dt_temp.Rows.Add(dt_Have.Rows(i).ItemArray)
                            '将以前最后一行数据加上
                            dt_temp.Rows.Add(dt_Have.Rows(i - 1).ItemArray)
                        Else
                            '如果不是当前选中行,则直接顺序复制到临时表中
                            dt_temp.Rows.Add(dt_Have.Rows(i).ItemArray)
                        End If
                    Else
                        '如果是向下运动
                        If CType(dt_Have.Rows(i)("CTID"), String) = Me.lbox_have.SelectedValue.ToString() And i <> dt_Have.Rows.Count - 1 Then
                            '如果是选中行,则将源datatable下一个元素与它作交换,
                            '先添加下一个元素
                            dt_temp.Rows.Add(dt_Have.Rows(i + 1).ItemArray)
                            '再添加选中元素
                            dt_temp.Rows.Add(dt_Have.Rows(i).ItemArray)
                            i = i + 1
                        Else
                            '如果不是当前选中行,则直接顺序复制到临时表中
                            dt_temp.Rows.Add(dt_Have.Rows(i).ItemArray)
                        End If
                    End If
                Next            ViewState("HaveDataTable") = dt_temp            '重新绑定
                band()            '如果是向上移动,则不允许超过顶部
                If _up = True And index > 0 Then
                    index = index - 1
                End If            '如果是向下移动,则不允许超过底部
                If _up = False And index < dt_Have.Rows.Count - 1 Then
                    index = index + 1
                End If            Me.lbox_have.SelectedIndex = index        Catch ex As Exception        End Try
        End Sub
    #End Region
      

  11.   

    waiting for the perfection answer!