我有一个数据表,其具有四列,如如何将GridView中的行向上和向下移动?
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时,第二行应该成为第一行,第一行应该成为第二行。就像明智的,当我选择第二行并单击向下ImageButton时,第二行应该成为第三行,第三行应该成为第二行。像明智的我必须将所有行向上和向下移动
如何实现此目的? 我曾尝试向上和向下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;
}
}
和我的GridView列的源代码如下:
<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中向上和向下移动整行?请帮帮我。
我想你应该正在寻找像设置'datatable1.DefaultView.Sort'属性根据你需要的列 – V4Vendetta
@ V4Vendetta:我试过dtUp.DefaultView.Sort =“SeqNbr”;但移动SeqNbr值时没有正确分配。 – thevan
你绑定了视图吗?底层数据类型是什么? – V4Vendetta