GridView排序 - 有没有更好的方法在asp.net中执行gridview排序(对于sharepoint)?

时间:2011-09-28 01:21:05

标签: asp.net sharepoint-2010

有没有更好的方法来对此数据网格进行排序?

这确实有效,但似乎有点不可重复使用。

如果您有任何想法或更好的方法来解决此问题,请告知我们。

以下是代码。如果您有任何想法,请告诉我。

    protected void grdPropMgrLeaseAgents_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = "A";

        if (ViewState[e.SortExpression + "_sort"] != null)
        {
            sortExpression = ViewState[e.SortExpression + "_sort"].ToString();
        }

        List<Person> list = (List<Person>)ViewState["propertyManagersAndLeasingAgents"];

        if (sortExpression == "A")
        {
            if (e.SortExpression == "EmpFullName") list.Sort((x, y) => string.Compare(x.EmpFullName, y.EmpFullName));
            if (e.SortExpression == "FirstName") list.Sort((x, y) => string.Compare(x.FirstName, y.FirstName));
            if (e.SortExpression == "LastName") list.Sort((x, y) => string.Compare(x.LastName, y.LastName));
            if (e.SortExpression == "DepartmentName") list.Sort((x, y) => string.Compare(x.DepartmentName, y.DepartmentName));
            if (e.SortExpression == "MarketName") list.Sort((x, y) => string.Compare(x.MarketName, y.MarketName));
            if (e.SortExpression == "Title") list.Sort((x, y) => string.Compare(x.Title, y.Title));
            if (e.SortExpression == "Roles") list.Sort((x, y) => string.Compare(x.Roles, y.Roles));
            if (e.SortExpression == "CellPhone") list.Sort((x, y) => string.Compare(x.CellPhone, y.CellPhone));
            if (e.SortExpression == "OfficePhone") list.Sort((x, y) => string.Compare(x.OfficePhone, y.OfficePhone));
            if (e.SortExpression == "EmailAddress") list.Sort((x, y) => string.Compare(x.EmailAddress, y.EmailAddress));
            if (e.SortExpression == "City") list.Sort((x, y) => string.Compare(x.City, y.City));
            if (e.SortExpression == "State") list.Sort((x, y) => string.Compare(x.State, y.State));
            if (e.SortExpression == "PropertyName") list.Sort((x, y) => string.Compare(x.PropertyName, y.PropertyName));
            if (e.SortExpression == "PropertyStreet") list.Sort((x, y) => string.Compare(x.PropertyStreet, y.PropertyStreet));
            if (e.SortExpression == "DisplayName") list.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName));
            if (e.SortExpression == "ZipCode") list.Sort((x, y) => string.Compare(x.ZipCode, y.ZipCode));
        }
        else
        {
            if (e.SortExpression == "EmpFullName") list.Sort((y, x) => string.Compare(x.EmpFullName, y.EmpFullName));
            if (e.SortExpression == "FirstName") list.Sort((y, x) => string.Compare(x.FirstName, y.FirstName));
            if (e.SortExpression == "LastName") list.Sort((y, x) => string.Compare(x.LastName, y.LastName));
            if (e.SortExpression == "DepartmentName") list.Sort((y, x) => string.Compare(x.DepartmentName, y.DepartmentName));
            if (e.SortExpression == "MarketName") list.Sort((y, x) => string.Compare(x.MarketName, y.MarketName));
            if (e.SortExpression == "Title") list.Sort((y, x) => string.Compare(x.Title, y.Title));
            if (e.SortExpression == "Roles") list.Sort((y, x) => string.Compare(x.Roles, y.Roles));
            if (e.SortExpression == "CellPhone") list.Sort((y, x) => string.Compare(x.CellPhone, y.CellPhone));
            if (e.SortExpression == "OfficePhone") list.Sort((y, x) => string.Compare(x.OfficePhone, y.OfficePhone));
            if (e.SortExpression == "EmailAddress") list.Sort((y, x) => string.Compare(x.EmailAddress, y.EmailAddress));
            if (e.SortExpression == "City") list.Sort((y, x) => string.Compare(x.City, y.City));
            if (e.SortExpression == "State") list.Sort((y, x) => string.Compare(x.State, y.State));
            if (e.SortExpression == "PropertyName") list.Sort((y, x) => string.Compare(x.PropertyName, y.PropertyName));
            if (e.SortExpression == "PropertyStreet") list.Sort((y, x) => string.Compare(x.PropertyStreet, y.PropertyStreet));
            if (e.SortExpression == "DisplayName") list.Sort((y, x) => string.Compare(x.DisplayName, y.DisplayName));
            if (e.SortExpression == "ZipCode") list.Sort((y, x) => string.Compare(x.ZipCode, y.ZipCode));
        }

        ViewState["propertyManagersAndLeasingAgents"] = list;

        this.grdPropMgrLeaseAgents.DataSource = list;
        this.grdPropMgrLeaseAgents.DataBind();

        if (sortExpression == "A")
        {
            ViewState[e.SortExpression + "_sort"] = "D";
        }
        else
        {
            ViewState[e.SortExpression + "_sort"] = "A";
        }
    }

1 个答案:

答案 0 :(得分:2)

你试过SPGridView吗?我使用了SPGridView和LinqDataSource。所有功能(排序,分页,过滤)都得到了处理。

这是一个例子: - 一个。 SpridView

 <SharePoint:SPGridView runat="server" ID="spgvUserTrainingLists" AutoGenerateColumns="false"
                                DataSourceID="linqDsEmployeeTrainingLists" DataKeyNames="RowId" OnRowDataBound="spgvUserTrainingLists_RowDataBound"
                                AllowSorting="true" AllowPaging="true" PageSize="15" AllowFiltering="true" FilteredDataSourcePropertyName="Where"
                                FilteredDataSourcePropertyFormat='{1} == "{0}"' FilterDataFields=",TrainingType,,Trainer,Status">
                                <Columns>
                                    <SharePoint:SPBoundField HeaderText="Ref #" SortExpression="RefNo" DataField="RefNo" />
                                    <SharePoint:SPBoundField HeaderText="Type" SortExpression="TrainingType" DataField="TrainingType" />
                                    <asp:TemplateField HeaderText="Training" SortExpression="TrainingTitle">
                                        <ItemTemplate>
                                            <asp:HyperLink ID="hlTrainingDetail" runat="server" Text='<%# Eval("TrainingTitle") %>'
                                                NavigateUrl="#" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <SharePoint:SPBoundField HeaderText="Trainer" SortExpression="Trainer" DataField="Trainer" />
                                    <SharePoint:SPBoundField HeaderText="Status" SortExpression="Status" DataField="Status" />
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:ImageButton ID="imgDelete" ImageUrl="~/_layouts/images/DELITEM.GIF" runat="server"
                                                UseSubmitBehaviour="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    No training yet.
                                </EmptyDataTemplate>
                            </SharePoint:SPGridView>
                            <SharePoint:SPGridViewPager ID="SPGridViewPager1" runat="server" GridViewId="spgvUserTrainingLists" />
                            <aspweb:LinqDataSource runat="server" ID="linqDsEmployeeTrainingLists" OnSelecting="linqDs_Selecting" />

湾SPGridView过滤符号

protected void spgvUserTrainingLists_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.Header)
        {
            if (!string.IsNullOrEmpty(spgvUserTrainingLists.FilterFieldName))
            {
                for (int i = 0; i < spgvUserTrainingLists.Columns.Count; i++)
                {
                    DataControlField field = spgvUserTrainingLists.Columns[i];

                    if (field.SortExpression == spgvUserTrainingLists.FilterFieldName)
                    {
                        Image filterIcon = new Image();
                        filterIcon.ImageUrl = "/_layouts/images/filter.gif";
                        filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";

                        Literal headerText = new Literal();
                        headerText.Text = field.HeaderText;

                        PlaceHolder panel = new PlaceHolder();
                        panel.Controls.Add(headerText);
                        panel.Controls.Add(filterIcon);

                        e.Row.Cells[i].Controls[0].Controls.Add(panel);

                        break;
                    }
                }
            }
        }
    }

℃。的LinqDataSource

 protected void linqDs_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        object parameter = null;

        if (e.SelectParameters.TryGetValue("employee", out parameter))
        {
            e.Result = DefaultBLL.GetEmployeeTrainingLists(parameter.ToString());
        }
    }

尝试使用SharePoint后尝试一下。 希望这会有所帮助。