100分不错哦,
我只能给你建议,暂无源码的
对gridview行当上下移动,你可以修改绑定gridview的数据源,譬如自己新建一列orderID,这里存放排列顺序,数据源根据此列排序,
你可以根据需要修改此列,如第一行和第二行的orderid值互相调换,起到第一行和第二行互相换了位置了.
补充:刚帮你找到了一段源码,用模板列,希望对你有帮助:)
Code
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 584px; POSITION: absolute; TOP: 112px" runat="server"
Text="Button">asp:Button>
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="392px">
<SelectedItemStyle BackColor="#FFFFC0">SelectedItemStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="lbtnUp" runat="server" CommandName="UP">上移↑asp:LinkButton>
<asp:LinkButton id="lbtnDown" runat="server" CommandName="DOWN">↓下移asp:LinkButton>
ItemTemplate>
asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="id">asp:BoundColumn>
<asp:BoundColumn DataField="content">asp:BoundColumn>
<asp:BoundColumn DataField="remark">asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="pri">asp:BoundColumn>
Columns>
asp:DataGrid>
form>
*********************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Threading;
namespace bsTest2005_8_16.CSDN
{
///
/// WebForm3 的摘要说明。
///
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindTestData();
}
}
private void BindTestData()
{
DataTable dt = CreateTable();
dt.Rows.Add(new object[]{1,"aaa","AAA",2});
dt.Rows.Add(new object[]{2,"bbb","BBB",1});
dt.Rows.Add(new object[]{3,"ccc","CCC",5});
dt.Rows.Add(new object[]{4,"ddd","DDD",4});
dt.Rows.Add(new object[]{5,"eee","eee",3});
dt.AcceptChanges();
DataView view = dt.DefaultView;
view.Sort = "PRI";
this.DataGrid1.DataSource = view;
this.DataGrid1.DataBind();
}
private DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Content");
dt.Columns.Add("Remark");
dt.Columns.Add("PRI");
return dt;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//最后把DataGrid里的数据保存到数据库
}
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName == "UP" || e.CommandName=="DOWN")
{
int index = e.Item.ItemIndex;
if(e.CommandName == "UP") index-=1;
else index+=1;
if(index<0 || index==DataGrid1.Items.Count) return;
DataTable dt = CreateTable();
for(int i=0;i<DataGrid1.Items.Count;i++)
{
if(i!=e.Item.ItemIndex)
{
DataGridItem row = DataGrid1.Items[i];
dt.Rows.Add(new object[]{row.Cells[1].Text,row.Cells[2].Text,row.Cells[3].Text,i+1});
}
}
DataRow moveRow = dt.NewRow();
moveRow[0] = e.Item.Cells[1].Text;
moveRow[1] = e.Item.Cells[2].Text;
moveRow[2] = e.Item.Cells[3].Text;
moveRow[3] = index+1;
dt.Rows.InsertAt(moveRow,index);
dt.AcceptChanges();
this.DataGrid1.SelectedIndex = index;
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
}
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex ==0)
{
((LinkButton)e.Item.FindControl("lbtnUp")).Enabled = false;
}
else
{
int totalItems = 0;
if(DataGrid1.DataSource is DataView) totalItems=((DataView)DataGrid1.DataSource).Count;
else if(DataGrid1.DataSource is DataTable) totalItems=((DataTable)DataGrid1.DataSource).Rows.Count;
if(e.Item.ItemIndex == totalItems-1)
((LinkButton)e.Item.FindControl("lbtnDown")).Enabled = false;
}
}
}
}