asp.net gridview的自定义排序数据源 [英] asp.net gridview sorting custom datasource

查看:150
本文介绍了asp.net gridview的自定义排序数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被填充好一个gridview。现在,我想启用排序。香港专业教育学院做了所有必要的code - 即实现排序上的标记并提供事件时,将调用用户各种

它,即时通讯与失去的那种事件 - 我试图从谷歌的几个实现,但即时通讯也不太清楚。从本质上讲是我corect地说,我需要提供新的查询到服务器根据用户需要排序和ASC或DESC也列了什么?果真如此的话听起来像很多的更多的工作....更多的查询。

谢谢
DAMO

code背后绑定的网

  //加载主网页数据网格
                    FAServices fServices =新FAServices(sConn);
                    FAAuditOverallStatusLatest fAuditOverallStatusLatest =新FAAuditOverallStatusLatest(sConn);
                    GridViewMain.DataSource = fAuditOverallStatusLatest.getAuditOverallStatusLatest();
                    GridViewMain.DataBind();

code后面的排序

 保护无效GridViewMain_Sorting(对象发件人,GridViewSortEventArgs E)
{    //切换此处需要语句与查询一起为每列我有网格
}

电网标记

 < ASP:GridView控件ID =GridViewMainOnRowDataBound =GridViewMainRowDataBoundOnPageIndexChanging =GridViewMain_PageIndexChanging
                                        =服务器AllowPaging =真每页=50PagerSettings-位置=TopAndBottom
                                        的CssClass =MGRID
                                        PagerStyle-的CssClass =PGR
                                        AlternatingRowStyle-的CssClass =ALT数据行
                                        OnRowCreated =GridViewMain_RowCreated
                                        RowStyle-的CssClass =数据行
                                        AllowSorting =真
                                        OnSorting =GridViewMain_Sorting
                                        >
                                     < / ASP:GridView的>


解决方案

  

我在说,我需要提供新的查询回corect
  服务器根据用户要排序什么样的列和ASC或
  DESC也?如果是这样,它听起来就像很多的更多的工作....更多的查询。


是的,你是正确的。您需要再次查询您的数据源,以应用新的排序。但最后一句是不正确的。你只需要修改 ORDER BY 您的SQL(或任何你使用订购数据源)。您可以使用一个为ViewState的变量,顺序列和方向(你需要在回传坚持它,所以ViewState中)。我会告诉你一个例​​子:

首先,你需要设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortex$p$pssion.aspx\"相对=nofollow> SORTEX pression 。

 &LT; ASP:的TemplateField的HeaderText =名称SORTEX pression =名称&GT;
    &LT;&ItemTemplate中GT;
        &所述; A HREF =sometest.aspx ID =&下;%#DataBinder.Eval的(的Container.DataItemTestID)的ToString()%方式&gt;'&GT;&下;%#DataBinder.Eval的(的Container.DataItem, 类型)的ToString()%方式&gt;&下; / A&GT;
    &LT; / ItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;
&LT; ASP:的TemplateField的HeaderText =地址SORTEX pression =地址&GT;
    &LT;&ItemTemplate中GT;
                &LT; D​​IV ALIGN =正确&GT;&LT;%#(DataBinder.Eval的(的Container.DataItem,HouseNumber))%GT;&LT; / DIV&GT;
    &LT; / ItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;

在codebehind可以存储当前的 SORTEX $ P $在pssion SortDirection 的ViewState

 私人字符串SORTEX $ P $ {pssion
    获得{
        如果(String.IsNullOrWhiteSpace((字符串)的ViewState [SORTEX pression])
            的ViewState [SORTEX pression] =名称ASC;        返回(字符串)的ViewState [SORTEX pression];
    }
    集合{的ViewState [SORTEX pression] =值; }
}

这里的排序事件处理程序。需要注意的是 BindGrid 是设置数据源的方式并调用 GridView.DataBind

 保护无效theGrid_Sorting(对象发件人,System.Web.UI.WebControls.GridViewSortEventArgs E)
{
    字符串currentSortColumn = NULL;
    字符串currentSortDirection = NULL;
    currentSortColumn = this.SortEx pression.Split('')[0];
    currentSortDirection = this.SortEx pression.Split('')[1];    如果(e.SortEx pression.Equals(currentSortColumn)){
        //切换排序方向
        开关(currentSortDirection.ToUpper()){
            案ASC:
                this.SortEx pression = currentSortColumn +DESC;
                打破;
            案DESC:
                this.SortEx pression = currentSortColumn +ASC;
                打破;
        }
    }其他{
        this.SortEx pression = e.SortEx pression +ASC;
    }    //这个SORTEX pression及的DataBind的电网负荷数据
    BindGrid();
}

i have a gridview that is being populated ok. Now i want to enable sorting. Ive done all the required code - namely enabling sorting on the markup and providing the event to call when a user sorts.

its the sort event that im lost with - i have tried a few implementations from Google but im not too sure. Essentially am i corect in saying that i need to provide new queries back to the server depending on what column the user wants to sort by and ASC or DESC also? if so it sounds like alot of more work....more queries.

thanks damo

Code Behind to Bind the grid

 // Load the main homepage data to the grid
                    FAServices fServices = new FAServices(sConn);
                    FAAuditOverallStatusLatest fAuditOverallStatusLatest = new FAAuditOverallStatusLatest(sConn);
                    GridViewMain.DataSource = fAuditOverallStatusLatest.getAuditOverallStatusLatest();
                    GridViewMain.DataBind();

Code behind to sort

protected void GridViewMain_Sorting(object sender, GridViewSortEventArgs e)
{

    // Switch statements required here along with Query for each column i have in the grid




}

Grid Markup

<asp:GridView ID="GridViewMain" OnRowDataBound="GridViewMainRowDataBound" OnPageIndexChanging="GridViewMain_PageIndexChanging"
                                        runat="server"  AllowPaging="True" PageSize="50" PagerSettings-Position="TopAndBottom"
                                        CssClass="mGrid"
                                        PagerStyle-CssClass="pgr"
                                        AlternatingRowStyle-CssClass="alt data-row"
                                        OnRowCreated="GridViewMain_RowCreated"                          
                                        RowStyle-CssClass="data-row"                                        
                                        AllowSorting="True"
                                        OnSorting="GridViewMain_Sorting"
                                        >
                                     </asp:GridView>

解决方案

Am i corect in saying that i need to provide new queries back to the server depending on what column the user wants to sort by and ASC or DESC also? If so it sounds like alot of more work....more queries.

Yes, you're correct. You need to query your datasource again to apply the new sort. But the last sentence is not correct. You just need to change the ORDER BY of your sql (or whatever you use to order the DataSource). You can use one ViewState variable for both, the order-column and the direction(you need to persist it across postbacks, therefore the ViewState). I'll show you an example:

First, you need to set the SortExpression.

<asp:TemplateField HeaderText="Name" SortExpression="Name">
    <ItemTemplate>
        <a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
    </ItemTemplate> 
</asp:TemplateField>    
<asp:TemplateField HeaderText="Address" SortExpression="Address">
    <ItemTemplate>
                <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
    </ItemTemplate> 
</asp:TemplateField>            

in the codebehind you can store the current SortExpression and the SortDirection in ViewState:

private string SortExpression {
    get {
        if(String.IsNullOrWhiteSpace((String)ViewState["SortExpression"])
            ViewState["SortExpression"] = "Name ASC";

        return (String)ViewState["SortExpression"];
    }
    set { ViewState["SortExpression"] = value; }
}

Here's the Sorting event handler. Note that BindGrid is the method where you set the DataSource and call GridView.DataBind

protected void theGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
    string currentSortColumn = null;
    string currentSortDirection = null;
    currentSortColumn = this.SortExpression.Split(' ')[0];
    currentSortDirection = this.SortExpression.Split(' ')[1];

    if (e.SortExpression.Equals(currentSortColumn)) {
        //switch sort direction
        switch (currentSortDirection.ToUpper()) {
            case "ASC":
                this.SortExpression = currentSortColumn + " DESC";
                break;
            case "DESC":
                this.SortExpression = currentSortColumn + " ASC";
                break;
        }
    } else {
        this.SortExpression = e.SortExpression + " ASC";
    }

    //load the data with this SortExpression and DataBind the Grid
    BindGrid();
}

这篇关于asp.net gridview的自定义排序数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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