手动绑定下的实现事件时间:2022-08-20 19:46:40from http://topic.csdn.net/u/20090818/15/898C6211-E412-477E-9FA0-40F6914096E5.htm#region Page_Load protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GridView1.Attributes.Add("SortExpression", "TestID"); this.GridView1.Attributes.Add("SortDirection", "ASC"); //绑定GridView fn_DataBind(); } } #endregion #region 得到绑定GridView的DataTable private DataTable fn_getDataTable() { string strSql = " SELECT TestID, TestUser, TestTime FROM Test "; DBClass db = new DBClass(); DataSet ds = db.RunSelectGetDataSet(strSql); return ds.Tables[0]; } #endregion #region 分页 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; fn_DataBind(); } #endregion #region 排序 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (e.SortExpression != "") { if (GridView1.PageCount > 0) { //设定排序方向 string SortDirection = "ASC"; SortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC"); this.GridView1.Attributes["SortExpression"] = e.SortExpression; this.GridView1.Attributes["SortDirection"] = SortDirection; //重新绑定数据 fn_DataBind(); } } } #endregion #region 绑定GridView private void fn_DataBind() { DataTable dt = fn_getDataTable(); string SortDirection = this.GridView1.Attributes["SortDirection"].ToString(); string SortExpression = this.GridView1.Attributes["SortExpression"].ToString(); dt.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection); this.GridView1.DataSource = dt; this.GridView1.DataBind(); } #endregion