繁体   English   中英

如何上下移动GridView中的行?

[英]How to move the rows in the GridView Up and Down?

我有一个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绑定。 现在,我有两个ImageButtons“ imgbtnUp”和“ imgbtnDown”,分别显示上下箭头图像。

当我选择GridView的第二行并单击Up ImageButton时,第二行应成为第一行,第一行应成为第二行。 同样,当我选择第二行并单击Down ImageButton时,第二行应成为第三行,第三行应成为第二行。 同样,我必须将所有行上下移动

如何实现呢? 我已经尝试了以下ImageUp和Down ImageButtons Click事件的编码:

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 

您可以只通过ToTable()方法将DataView的更改应用于数据表,而ToTable()基于gridview更新数据表。

例如:

DataTable myData;
myData = myData.DefaultView.ToTable();

这是我的问题的理想答案。

protected void imgbtnUp_Click(object sender, ImageClickEventArgs e)
{   
    DataTable dt = new DataTable();
    DataTable dtnew = new DataTable();    
    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
        {
            dt = (DataTable)ViewState["Template"];//dt is the DataTable I mentioned above
            int value = Convert.ToInt32(dt.Rows[index]["SeqNbr"].ToString());
            dt.Rows[index]["SeqNbr"] = Convert.ToInt32(index) - 1;
            dt.Rows[index - 1]["SeqNbr"] = value;// Convert.ToInt32(index);
            dt.DefaultView.Sort = "SeqNbr";
            dt.AcceptChanges();
            dtnew = dt.Copy();
            gvMileStone.DataSource = dt;
            gvMileStone.DataBind();
            dt.AcceptChanges();
            for (int i = 0; i <= gvMileStone.Rows.Count - 1; i++)
            {
                dtnew.Rows[i]["MileStoneID"] = Convert.ToInt32(((Label)gvMileStone.Rows[i].FindControl("lblMileStoneID")).Text);
                dtnew.Rows[i]["MileStoneName"] = gvMileStone.Rows[i].Cells[1].Text;
                dtnew.Rows[i]["Percentage"] = Convert.ToDecimal(((TextBox)gvMileStone.Rows[i].FindControl("txtPercentage")).Text);
                dtnew.Rows[i]["SeqNbr"] = Convert.ToInt32(((Label)gvMileStone.Rows[i].FindControl("lblSeqNbr")).Text);
            }
            ViewState["Template"] = dtnew;
        }
   }
   else
   {
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('There is no rows to move!')", true);
        return;
   }
}

它只是更改单个单元格,因为您只是告诉它更改单个单元格。 该行仍具有相同的索引。 要更改行的位置,您需要对正确的值进行排序。 (我的直觉告诉我您是根据MileStoneID而不是SeqNbr排序的?)

以下是向上按钮:

   if (gvMileStone.Rows.Count > 0)
    {
        int index = gvMileStone.SelectedIndex;
        if (index != 0)
        {
            DataTable dtUp = (DataTable)ViewState["Template"];
            int value = Convert.ToInt32(dtUp.Rows[index]["SeqNbr"].ToString());
            dtUp.Rows[index]["SeqNbr"] = value - 1;
            dtUp.Rows[index - 1]["SeqNbr"] = value;
            dtUp.DefaultView.Sort = "SeqNbr";                   
            dtUp.AcceptChanges();
            gvMileStone.DataSource = dtUp;
            gvMileStone.DataBind();
            ViewState["Template"] = dtUp;
        }
    }

应该管用? 只要确保您不在第一行即可。;)

DataRow dr = dt.Rows[selectedIndex];
ArrayList drList = new ArrayList();
for (int i = 0; i < dt.Columns.Count; i++)
{
    drList.Add(dr[i]);
}

dt.Rows[selectedIndex].Delete();
dt.AcceptChanges();

DataRow drNew = dt.NewRow();

for (int i = 0; i < drList.Count; i++)
{
    drNew[i] = drList[i];
}

drList = null;

dt.Rows.InsertAt(drNew, (selectedIndex));
dt.AcceptChanges();

试试这个

gvMileStone.DataSource = dtUp.DefaultView;

暂无
暂无

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

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