我有一个包含四列的数据表,例如
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 绑定(bind)。现在我有两个 ImageButtons“imgbtnUp”和“imgbtnDown”,它们显示向上和向下箭头图像。
当我选择 GridView 的第二行并单击向上图像按钮时,第二行应成为第一行,第一行应成为第二行。同样,当我选择第二行并单击 Down ImageButton 时,第二行应该变成第三行,第三行应该变成第二行。同样明智的是,我必须上下移动所有行
如何实现? 我已经为向上和向下 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;
}
}
而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
关于c# - 如何上下移动 GridView 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994371/