手机
当前位置:查字典教程网 >编程开发 >C#教程 >Gridview自动排序功能的实现
Gridview自动排序功能的实现
摘要:注意两点:1.要将gridview的AllowSorting属性置为true,同时设置OnSorting事件2.在OnSorting事件中对...

注意两点:

1.要将gridview的AllowSorting属性置为true,同时设置OnSorting事件

2.在OnSorting事件中对排序的列设定SortExpression属性

复制代码 代码如下:

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

if (Session["Admin"] != "admin")

{

//如果会话过期,则应该重新登录

this.Response.Write(" <script language=javascript>alert('你无权访问该页面,请与管理员联系!');window.location.href='../UserLogin.aspx';</script> ");

}

复制代码 代码如下:

ViewState["sortExpression"] = "Isdistribution";

ViewState["sort"] = " ASC";

}

//绑定信息

BindNodeInfo();

}

public void BindNodeInfo()

{

NodeLogic log = new NodeLogic();

DataSet myset = log.GetNodeInfo(); //获取数据源

DataView myview = myset.Tables[0].DefaultView;

myview.Sort = ViewState["sortExpression"].ToString() +" "+ ViewState["sort"].ToString();

this.NodeGridView.DataSource = myview;

NodeGridView.DataKeyNames = new string[] { "node_id" }; //设置主键字段

NodeGridView.DataBind(); //绑定GridView控件

}

protected void NodeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

this.NodeGridView.PageIndex = e.NewPageIndex;

BindNodeInfo();

}

protected void NodeGridView_RowDataBound(object sender, GridViewRowEventArgs e)

{

// 自动给第一列编号

if (e.Row.RowIndex > -1)

{

e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);

}

}

protected void NodeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

NodeLogic log = new NodeLogic();

int id = int.Parse(this.NodeGridView.DataKeys[e.RowIndex].Values[0].ToString());

if (log.DeleteNodeInfo(id))

{

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功!');", true);

}

else

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败!');", true);

//重新更新数据显示

BindNodeInfo();

}

protected void NodemGridView_RowEditing(object sender, GridViewEditEventArgs e)

{

}

protected void AddNode_Click(object sender, EventArgs e)

{

Response.Redirect("AddNode.aspx");

}

protected void NodeGridView_Sorting(object sender, GridViewSortEventArgs e)

{

if (ViewState["sortExpression"] != null)

{

if (ViewState["sort"].ToString() == "Asc")

{

ViewState["sort"] = "Desc";

}

else

{

ViewState["sort"] = "Asc";

}

}

BindNodeInfo();

}

【Gridview自动排序功能的实现】相关文章:

C#连接db2数据库的实现方法

C# 利用StringBuilder提升字符串拼接性能的小例子

c# list部分操作实现代码

C#启动和停止windows服务的实例代码

解析c#显示友好时间的实现代码

C# Guid.NewGuid获得随机数

C# 语音功能的实现方法

C# SendInput 模拟鼠标操作的实现方法

C#加密解密文件小工具实现代码

将DLL放入到资源中,运行时自动加载的小例子

精品推荐
分类导航