如何上下移动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 绑定。现在我有两个图像按钮“imgbtnUp”和“imgbtnDown”,它们显示向上和向下箭头图像。
当我选择 GridView 的第二行并单击向上图像按钮时,第二行应成为第一行,第一行应成为第二行。同样,当我选择第二行并单击向下图像按钮时,第二行应成为第三行,第三行应成为第二行。同样,我必须上下移动所有行
如何实现这一点? 我已尝试对向上和向下 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;
}
}
我的 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 中上下移动整行?请帮我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个。
您已选择存储在索引变量中的索引。
现在创建一个
Try this.
you have selected index stored in index variable.
now create one
这是我的问题想要的答案。
This is the desired answer for my question.
您可以通过
ToTable()
方法将 DataView 的更改应用到数据表,而不是根据 gridview 逐一更新数据表,例如:
Instead of doing updating datatable based on gridview one by one, you can just apply the changes of DataView to data table by
ToTable()
methodEg:
它只更改一个单元格,因为您只告诉它更改一个单元格。该行仍然具有相同的索引。要使行改变位置,您需要按正确的值进行排序。 (我的直觉告诉我,您正在 MileStoneID 上排序,而不是在 SeqNbr 上排序?)
以下是向上按钮:
应该有效吗?只要确保您不在顶排..;)
It's only changing a single cell because you are only telling it to change a single cell. The row still has the same index. To make the rows change places, you need to sort on the correct value. (My gut is telling me that you are sorting on MileStoneID and not on SeqNbr?)
the following is for the up button:
should work? Just make sure you are not on the top row.. ;)
试试这个
Try this one