本文介绍了如何向上和向下移动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在网格视图之外单击按钮(向上/向下)上下移动所选行。
请帮帮我。
I want to move the selected row Up and down on click of Buttons (Up/Down) which are outside the gridview.
Please help me.
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt =GetTable();
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["Table"] = dt;
}
}
protected void ButtonUp_Click(object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
if (index >0)
{
DataTable dtUp = (DataTable)ViewState["Table"];
DataRow row = dtUp.NewRow();
row.ItemArray = dtUp.Rows[index].ItemArray;
dtUp.Rows.RemoveAt(index);
dtUp.Rows.InsertAt(row, index - 1);
dtUp.AcceptChanges();
GridView1.DataSource = dtUp;
GridView1.DataBind();
ViewState["Table"] = dtUp;
}
}
protected void ButtonDown_Click(object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
if (index < GridView1.Rows.Count)
{
DataTable dtdown = (DataTable)ViewState["Table"];
DataRow row = dtdown.NewRow();
row.ItemArray = dtdown.Rows[index].ItemArray;
dtdown.Rows.RemoveAt(index);
dtdown.Rows.InsertAt(row, index + 1);
dtdown.AcceptChanges();
GridView1.DataSource = dtdown;
GridView1.DataBind();
ViewState["Table"] = dtdown;
}
}
这篇关于如何向上和向下移动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!