如何将GridView中的行向上移动和向下移动? [英] How to move the rows in the GridView Up and Down?

查看:119
本文介绍了如何将GridView中的行向上移动和向下移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,它有四列,如
$ b $ pre $ MileStoneID MileStoneName百分比SeqNbr
----- -------- --------------- ------------ ------
1 M-One 25 1
2 M-Two 30 2
3 M-Three 50 3
10 M-Four 20 4

我将此数据表与一个GridView绑定。现在我有两个ImageButtonsimgbtnUp和imgbtnDown,它显示上下箭头图像。



当我选择GridView的第二行并单击Up ImageButton时,那么第二行应该成为第一行,第一行应该成为第二行。就像明智的,当我选择第二行并单击向下ImageButton时,第二行应该成为第三行,第三行应该成为第二行。像智者一样,我必须将所有行向上和向下移动



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

  protected void imgbtnUp_Click(object sender,如果(gvMileStone.Rows.Count> 0)
{
int index = gvMileStone.SelectedIndex;
if(index == 0)
{
ScriptManager.RegisterClientScriptBlock(this.Page,typeof(UpdatePanel),Guid.NewGuid()。ToString(),window.alert('You不能移动记录!'),true);
return;
}
else
{
DataTable dtUp =(DataTable)ViewState [Template]; // dtUp是我上面提到的DataTable
int value = Convert。 ToInt32(dtUp.Rows [指数] [ 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;

$ b $ else

ScriptManager.RegisterClientScriptBlock(this.Page,typeof(UpdatePanel),Guid.NewGuid()。ToString(),窗口。 alert('没有行移动!'),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('你不能移动记录!'),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;

$ b $ else

ScriptManager.RegisterClientScriptBlock(this.Page,typeof(UpdatePanel),Guid.NewGuid()。ToString(),窗口。 alert('没有行移动!'),true);
return;
}
}

和My GridView Column的源代码如下:

 <列> 
< asp:TemplateField Visible =false>
< ItemTemplate>
< asp:Label ID =lblMileStoneIDrunat =serverText ='<%#Bind(MileStoneID)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =MileStoneNameHeaderText =MileStone NameSortExpression =MileStoneNameItemStyle-Width =130px/>
< asp:TemplateField HeaderText =Percentage>
< ItemTemplate>
< asp:TextBox ID =txtPercentagerunat =serverText ='<%#Bind(Percentage)%>'Width =50pxonkeypress =return DecimalValidate(event); >< / ASP:文本框>
< / ItemTemplate>
< ItemStyle Width =100px/>
< ControlStyle Width =100px/>
< / asp:TemplateField>
< asp:TemplateField Visible =false>
< ItemTemplate>
< asp:Label ID =lblSeqNbrrunat =serverText ='<%#Bind(SeqNbr)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField Visible =false>
< ItemTemplate>
< asp:LinkBut​​ton ID =lnkSelectrunat =serverCommandName =selectText =select/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>

起初,某些行正在正确移动,但在两三次点击事件后,它无法正常工作。如何在GridView中向上和向下移动整行?请帮助我。

解决方案

这是我的问题所需的答案。

  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不能移动记录!'),true);
return;
}
else
{
dt =(DataTable)ViewState [Template]; // dt是我上面提到的DataTable
int value = Convert.ToInt32 (dt.Rows [指数] [ 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 ((标签)gvMileStone.Rows [I] .FindControl( lblMileStoneID))的文本)。
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;

$ b $ else

ScriptManager.RegisterClientScriptBlock(this.Page,typeof(UpdatePanel),Guid.NewGuid()。ToString(),窗口。 alert('没有行移动!'),true);
return;
}
}


I have one DataTable which has four columns such as

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

I bind this datatable with one GridView. Now I have two ImageButtons "imgbtnUp" and "imgbtnDown" which shows Up and Down Arrow Image.

When I select the second row of the GridView and Clicks the Up ImageButton, then the second row should become the first row and the first row should become the second row. Like wise when I select the second row and clicks the Down ImageButton, then the second row should become the third row and third row should become the second row. Like wise I have to move the all the rows Up and Down

How to achieve this? I have tried the following coding for the Up and Down ImageButtons Click Event:

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;
        }
 }

and My GridView Column's source code is as follows:

<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>                                    

At first, some rows are moving correctly, but after two or three click events its not working. How to move the Entire row Up and Down in the GridView? please help me.

解决方案

This is the desired answer for my question.

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;
   }
}

这篇关于如何将GridView中的行向上移动和向下移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆