This is a migrated thread and some comments may be shown as answers.

[Solved] Beginners (?) problem with sorting

4 Answers 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
melkorman
Top achievements
Rank 1
melkorman asked on 01 Oct 2009, 01:08 PM
To simplify for you, here is (most of) the markup. As you can see it is pretty straight forward and simple, but it doesn't work:

<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <telerik:RadSkinManager ID="RadSkinManager1" Runat="server" Skin="WebBlue">
    </telerik:RadSkinManager>
    <telerik:RadFormDecorator ID="RadFormDecorator" runat="server" DecoratedControls="All">
    </telerik:RadFormDecorator>
    <div>
        <telerik:RadGrid ID="DocumentGrid" runat="server" Width="100%" OnNeedDataSource="DocumentGrid_NeedDataSource" AllowPaging="true" AllowSorting="true" OnSortCommand="DocumentGrid_SortCommand">
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>


And the code behind:

namespace WebApplication1
{
    [Serializable]
    public class Document
    {
        public int Id { get; set; }
        public string ObjectId { get; set; }
        public string ArchiveCode { get; set; }
        public DateTime CreateTime { get; set; }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Document> documents = new List<Document>();
                DateTime theTime = DateTime.Now;
                documents.Add(new Document() { Id = 1, ObjectId = "12345", ArchiveCode = "X", CreateTime = theTime });
                documents.Add(new Document() { Id = 2, ObjectId = "12345", ArchiveCode = "Y", CreateTime = theTime.AddDays(1) });
                documents.Add(new Document() { Id = 3, ObjectId = "12346", ArchiveCode = "Y", CreateTime = theTime });

                ViewState["Documents"] = documents;

                DocumentGrid.PageSize = 3;
                DocumentGrid.AutoGenerateColumns = false;
                DocumentGrid.MasterTableView.RetrieveAllDataFields = false;
                DocumentGrid.MasterTableView.AllowMultiColumnSorting = true;

                GridSortExpression sortExpression = new GridSortExpression() { FieldName = "Id", SortOrder = GridSortOrder.Descending };
                DocumentGrid.MasterTableView.SortExpressions.AddSortExpression(sortExpression);
            }
        }

        protected void DocumentGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            DocumentGrid.Columns.Clear();
            DocumentGrid.Columns.Add(new GridBoundColumn() { DataField = "Id", HeaderText = "Id", UniqueName = "DocumentIdColumn", SortExpression = "Id" });
            DocumentGrid.Columns.Add(new GridBoundColumn() { DataField = "ObjectId", HeaderText = "ObjectId", UniqueName = "DocumentObjectIdColumn", SortExpression = "ObjectId" });
            DocumentGrid.Columns.Add(new GridBoundColumn() { DataField = "CreateTime", HeaderText = "CreateTime", UniqueName = "DocumentCreateTimeColumn", SortExpression = "CreateTime" });
            DocumentGrid.Columns.Add(new GridBoundColumn() { DataField = "ArchiveCode", HeaderText = "ArchiveCode", UniqueName = "DocumentArchiveCodeColumn", SortExpression = "ArchiveCode" });

            DocumentGrid.DataSource = (List<Document>)ViewState["Documents"];
        }

        protected void DocumentGrid_SortCommand(object source, GridSortCommandEventArgs e)
        {
            GridSortExpression sortExpr = new GridSortExpression();
            switch (e.OldSortOrder)
            {
                case GridSortOrder.None:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = GridSortOrder.Descending;

                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
                case GridSortOrder.Ascending:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = DocumentGrid.MasterTableView.AllowNaturalSort ? GridSortOrder.None : GridSortOrder.Descending;
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
                case GridSortOrder.Descending:
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = GridSortOrder.Ascending;

                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                    break;
            }

            e.Canceled = true;
            DocumentGrid.Rebind();
        }
    }
}

Can someone please tell me what is wrong with this code?

4 Answers, 1 is accepted

Sort by
0
melkorman
Top achievements
Rank 1
answered on 05 Oct 2009, 08:31 AM
Darn, I accidentally deleted the first post I made, so here is a recap:

I have this simple project (see code) where I try to implement sorting for a radgrid. When I click a column the only thing that happens is that all the column headers vanishes and the list remains unsorted. What could be wrong? As you can see, there is nothing advanced going on (at least not in my code).

I will gladly send you the small project if you would like to test it out for your self.

Best regards
0
Accepted
Daniel
Telerik team
answered on 07 Oct 2009, 09:16 AM
Hello Мelkorman,

I created a sample project based on the code-snippets you posted below. Please test it locally and let me know whether it behaves as expected. In the meantime I strongly recommend you examine the following links:
RadGrid Programmatic creation
Programmatic creation on PageLoad
Programmatic creation on PageInit

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
melkorman
Top achievements
Rank 1
answered on 08 Oct 2009, 07:15 AM
Thanks for the info and the links!

I tried your code out and it boiled down to the difference between my code:

DocumentGrid.Columns.Add(new GridBoundColumn() 
   { DataField = "Id",  
     HeaderText = "Id",  
     UniqueName = "DocumentIdColumn",  
     SortExpression = "Id"  
   }); 

and yours:

GridBoundColumn gbc = new GridBoundColumn(); 
DocumentGrid.Columns.Add(gbc); 
gbc.DataField = "Id"
gbc.HeaderText = "Id"
gbc.UniqueName = "DocumentIdColumn"
gbc.SortExpression = "Id"

Oddly enough, it appears as if you have to add the column before you set its properties. Is that it?


0
Sebastian
Telerik team
answered on 08 Oct 2009, 08:54 AM
Hello melkorman,

That is correct. Refer to the resources linked from my previous message for further details.

Kind regards,
Daniel,
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
melkorman
Top achievements
Rank 1
Answers by
melkorman
Top achievements
Rank 1
Daniel
Telerik team
Sebastian
Telerik team
Share this question
or