VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之Gridview自动排序功能的实现

注意两点:
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();
        }



相关教程