C#代码在GridView中对数据进行排序 [英] c# code for sorting data in gridview

查看:102
本文介绍了C#代码在GridView中对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#代码,无需单击任何按钮即可在页面加载时在gridview中对数据进行排序.数据是从sql server 2005数据库中填充的.

c# code for sorting data in gridview on page load without clicking any button .data is filled from sql server 2005 database

推荐答案

从数据库中填充数据时,您应该在查询中使用"order by"子句来获取排序后的数据,并在页面加载时调用该方法(包含查询).
As data is filled from database, you should use ''order by'' clause in your query for getting sorted data and call that method(containing the query) in page load.


来源:
<asp:GridView ID="gridUserMaster" runat="server" AllowSorting="True" OnSorting="CustomersGridView_Sorting" 


CS:


CS:

protected void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
    {
        try
        {
            DataTable datatable = (DataTable)ViewState["gridview_datasource"];
            if (datatable != null)
            {
                DataView dataview = new DataView(datatable);
                if ((ViewState["sortdirection"] == null || (SortDirection)ViewState["sortdirection"] == SortDirection.Descending))
                    ViewState["sortdirection"] = SortDirection.Ascending;
                else
                    ViewState["sortdirection"] = SortDirection.Descending;
                dataview.Sort = e.SortExpression + " " + convertsortdirection((SortDirection)ViewState["sortdirection"]);
                gridUserMaster.DataSource = dataview;
                gridUserMaster.DataBind();
            }
        }
        catch (Exception ex)
        {
            Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
            ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + ex.Message.ToString() + "');", true);
        }
    }



获取有关MSDN的更多详细说明:
ASP.NET 2.0的GridView示例:对GridView的数据进行分页和排序 [ ^ ]
在GridView Web服务器控件中排序数据 [如何在asp.net中的GridView中对数据进行排序 [^ ]
如何将数据分类到GridView [



Get more detailed description on MSDN:
GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView''s Data[^]
Sorting Data in a GridView Web Server Control[^]

Similar thread:
How to sort data in GridView in asp.net[^]
How to sort data into GridView[^]


这篇关于C#代码在GridView中对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆