Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用实体数据源时如何对gridview进行排序?_Gridview_Entity Framework 6 - Fatal编程技术网

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

使用实体数据源时如何对gridview进行排序?,gridview,entity-framework-6,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"];

我有一个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标题时,什么都没有发生! 谁知道我的错在哪里