gridview自动排序示例分享(2)

// 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

this.BindGridView();
    }

/// <summary>
    /// 绑定到GridView
    /// </summary>
    private void BindGridView()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];

// 调用业务数据获取方法
        DataTable dtBind = this.getDB();

// 根据GridView排序数据列及排序方向设置显示的默认数据视图
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
        }

// GridView绑定并显示数据
        this.GridView1.DataSource = dtBind;
        this.GridView1.DataBind();
    }

/// <summary>
    /// 获取数据源的方法
    /// </summary>
    /// <returns>数据源</returns>
    private DataTable getDB()
    {
        DataTable dt = new DataTable();

dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt.Columns.Add("age");

dt.Rows.Add(new object[] { "000001", "hekui", "26" });
        dt.Rows.Add(new object[] { "000002", "zhangyu", "26" });
        dt.Rows.Add(new object[] { "000003", "zhukundian", "27" });
        dt.Rows.Add(new object[] { "000004", "liyang", "25" });
        dt.Rows.Add(new object[] { "000005", "caili", "27" });

return dt;
    }
}

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzpgs.html