我想通过点击按钮上下移动gridview行,然后按照
进行操作http://www.aspdotnet-suresh.com/2012/06/move-aspnet-gridview-rows-up-and-down.html
它工作正常。但是,我在gridview上启用了排序。列排序时,行不会上下移动。有人可以建议如何实现它,以便即使列已排序,网格视图行也会上下移动。
谢谢,
答案 0 :(得分:2)
这是在网格视图中向上和向下移动行的另一种方法。
if (e.CommandName == "UP")
{
Button btnup = (Button)sender;
GridViewRow row = (GridViewRow)btnup.NamingContainer;
var rows = id.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();
switch (e.CommandName)
{
case "UP":
if (row.RowIndex.Equals(0))
rows.Add(row);
else
rows.Insert(row.RowIndex - 1, row);
break;
case "Down":
if (row.RowIndex.Equals(id.Rows.Count - 1))
rows.Insert(0, row);
else
rows.Insert(row.RowIndex + 1, row);
break;
}
}
答案 1 :(得分:1)
Old Post但没有足够的回复评论,Pratiks的回答只会触发UP命令,因为Down命令是在第一个if语句中构建的......它需要。
Button btnup = (Button)sender;
GridViewRow row = (GridViewRow)btnup.NamingContainer;
var rows = id.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();
if (e.CommandName == "UP")
{
if (row.RowIndex.Equals(0)) {
rows.Add(row);}
else {
rows.Insert(row.RowIndex - 1, row); }
}
}
else if (e.CommandName == "DOWN")
{
if (row.RowIndex.Equals(id.Rows.Count - 1)) {
rows.Insert(0, row);}
else {
rows.Insert(row.RowIndex + 1, row);}
}