GridView Sort - Есть ли лучший способ выполнить сортировку сетки в asp.net (для sharepoint)? - PullRequest
0 голосов
/ 28 сентября 2011

Есть ли лучший способ отсортировать эту сетку данных?

Это работает, однако кажется, что его нельзя использовать повторно.

Если у вас есть идеи или более эффективные способы решения этой проблемы, пожалуйста, дайте мне знать.

Ниже приведен код. Пожалуйста, дайте мне знать, если у вас есть идеи ..

    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 Ответ

2 голосов
/ 28 сентября 2011

Вы пробовали 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. Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...