简体   繁体   English

在asp.net页面的gridview上排序不起作用

[英]sorting is not working on gridview in asp.net page

I use a gridview on my asp.net page. 我在asp.net页面上使用gridview。 And write a code for sorting, but the problem is that the sorting is not working. 并编写用于排序的代码,但问题是排序无法正常进行。 Can you please tell me what i am doing mistake. 你能告诉我我在做什么错。

Code for bind the gridview 绑定GridView的代码

DataSet _ds = _fOrderWrapper.ExecuteDataSet();
   ViewState["FOrders"] = _rows;
   lblFinalisedCount.Text = _ds.Tables[0].Rows.Count.ToString();
   GridOpen.DataSource = _ds.Tables[0];
   ViewState["dt"] = _ds.Tables[0];
   ViewState["sort"] = "ASC";
    GridOpen.DataBind();
    UpdatePanel1.Update();

Sorting Event code : 排序事件代码:

try
    {
        DataTable dt1 = (DataTable)ViewState["dt"];
        if (dt1.Rows.Count > 0)
        {
            if (Convert.ToString(ViewState["sort"]) == "ASC")
            {
                dt1.DefaultView.Sort = e.SortExpression +" " + "DESC";
                ViewState["sort"] = "Desc";
            }
            else
            {
                dt1.DefaultView.Sort = e.SortExpression +" "+ "ASC";
                ViewState["sort"] = "ASC";
            }
            GridOpen.DataSource = dt1;
            GridOpen.DataBind();
            UpdatePanel1.Update();
        }
    }
    catch (Exception ex)
    {
    }

Your code might be hitting the sorting event code first, and then executing the regular data binding code second. 您的代码可能先击中排序事件代码,然后再执行常规数据绑定代码。 The second data binding wipes out the effect of the first data binding. 第二个数据绑定消除了第一个数据绑定的影响。 Try putting breakpoints in each location. 尝试在每个位置放置断点。 When ASP.NET processes a request for an update panel, it runs the entire page life-cycle, not merely the event handler for the thing that triggered the update. 当ASP.NET处理对更新面板的请求时,它将运行整个页面生命周期,而不仅仅是触发更新的事件的事件处理程序。

EDIT after further review: 经过进一步审查后编辑:

Your code does this: 您的代码执行此操作:

dt1.DefaultView.Sort = ...

Which changes the sorting for the default DataView associated with the DataTable. 这将更改与DataTable关联的默认DataView的排序。

But then it sets the data source for the gridview to the DataTable itself. 但是,随后它将gridview的数据源设置为DataTable本身。

GridOpen.DataSource = dt1;

The Default DataView is not being used for data binding, I believe. 我相信Default DataView并未用于数据绑定。 (Been a while since I've done this, I might still be wrong). (自从这样做以来已有一段时间,我可能还是错了)。

I think you need to bind the GridView to the default DataView: 我认为您需要将GridView绑定到默认的DataView:

GridOpen.DataSource = dt1.DefaultView;

I think the reason the DefaultView is not automatically used when binding to the DataTable is that you would not otherwise have any way to bypass a DataView when binding to the data source. 我认为绑定到DataTable时不会自动使用DefaultView的原因是,否则绑定到数据源时您将没有任何绕过DataView的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM