在使用实体数据源时如何对gridview进行排序?

时间:2015-12-23 10:26:08

标签: gridview entity-framework-6

我有一个Gridview女巫有一个实体数据源我试图按用户按任意列的标题排序列,所以我写了这段代码:

protected void Page_Load(object sender, EventArgs e)
{

    griddata();
}

private void griddata()
{
    string name = Request.QueryString["name"];
    string Tel = Request.QueryString["Tel"];
    string Address = Request.QueryString["Address"];
    string Description = Request.QueryString["Description"];
    int? CaseNumber = string.IsNullOrEmpty(Request.QueryString["CaseNumber"]) ? (int?)null : Convert.ToInt16(Request.QueryString["CaseNumber"]);


    dbModel db = new dbModel();
    var datasource = from source in db.Files
                     where
                            (source.Name == null || source.Name.Contains(name)) &&
                            (source.Tel == null || source.Tel.Contains(Tel)) &&
                            (source.Address == null || source.Address.Contains(Address)) &&
                            (source.Description == null || source.Description.Contains(Description)) &&
                            (CaseNumber == null || source.CaseNumber == (CaseNumber)) &&

                     select source;

    GridView1.DataSource = datasource.ToList();
    GridView1.DataBind();
    DataTable dtbl = GridView1.DataSource as DataTable;
    ViewState["dbl"] = dtbl;
}

我的分类代码是:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = ViewState["dbl"] as DataTable;
    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

        GridView1.DataSource = dataView;
        GridView1.DataBind();
    }

}
private string ConvertSortDirection(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;
    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

        case SortDirection.Descending:
            newSortDirection = "DESC";
            break;
    }

    return newSortDirection;
}

但是当我按下gridview标题时,没有任何事情发生! 任何人都知道我的错误在哪里?

0 个答案:

没有答案