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

cannot get grid sorting/paging to work with declarative datasource and nhibernate

3 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kieran
Top achievements
Rank 1
Kieran asked on 27 Oct 2010, 11:32 PM
Hi

I have a page set up as below. All methods are correctly wired to the grid in the .aspx page. as shown here

<telerik:RadGrid ID="FilesGrid" runat="server" AllowPaging="True"
                AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
                onpageindexchanged="FilesGrid_PageIndexChanged"
                onsortcommand="FilesGrid_SortCommand" Skin="Outlook"
                style="font-style: italic"
                onitemdatabound="FilesGrid_ItemDataBound">
                <MasterTableView AllowNaturalSort="false">
                </MasterTableView>
        </telerik:RadGrid>

However I am getting very inconsistent behaviour. I am not sure if it is something silly I am doing or is a result of using a nhibernate dataset.
For most fields field that map directly to a read/write nhibernate property it all works ok. However some columns need to click twice to sort them. Then I have what is a calculated nhinernate property of type sting - this sorts but will only sort one way??
Then when I try to use the standard navigator to page, it will go to page 2 but gets stuck there.

Any ideas? Also how - if possible - would I use a linked nhibernate property to sort.ie Status.StatusDescription? If not possible could I implement custom sorting? But how to keep the sorted columns highlighted even when underlying field being sorted is different?

Any pointers as always gratefully appreciated. Really at end of my tether and this is slowing the project right down, if can't fix it will have to find another solution

Thanks in advance
Kieran

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate.Cfg;
using Telerik.Web.UI;
using NHibernate;

public partial class Files : BasePageMinor
{
    private string Sorting = string.Empty;
    private string Sortdir = string.Empty;


    protected void Page_Init(object sender, EventArgs e)
    {
        State.UseState = true;

        Master.SetPage();

    }


    protected void Page_Load(object sender, EventArgs e)
    {
        SetUpGrid();

    }

 
    private void SetUpGrid()
    {
        FilesGrid.MasterTableView.Columns.Clear();

        FilesGrid.MasterTableView.DataKeyNames = new string[] { "FileId", "FileRef" };

        GridBoundColumn _col;

        string sortField = State.FileOrderField??string.Empty;
        string sortDir = State.FileOrderDir ?? "asc";


        GridButtonColumn _imagecol = new GridButtonColumn();
        FilesGrid.MasterTableView.Columns.Add(_imagecol);
        _imagecol.UniqueName = "fileimagestatus";
        _imagecol.ButtonType = GridButtonColumnType.ImageButton;
        _imagecol.Resizable = false;
        _imagecol.ItemStyle.Width = 10;

        _col = new GridBoundColumn();
        FilesGrid.MasterTableView.Columns.Add(_col);
        _col.DataField = "BorrowStatusString";
        _col.UniqueName = "BorrowStatusString";
        _col.DataType = Type.GetType("System.String");
        _col.Display = false;

        

        foreach (FieldDisplay fd in new FileDisplay(s.GetParams()))
        {
            if (!(fd.Display == FieldShow.Show))
                continue;


            if (sortField == string.Empty)
                sortField = fd.nHibernateMapping;

            _col = new GridBoundColumn();
            FilesGrid.MasterTableView.Columns.Add(_col);
            _col.HeaderText = fd.HeaderDisplay;
            _col.DataField = fd.nHibernateMapping;
            _col.UniqueName = fd.nHibernateMapping;
            _col.DataType = Type.GetType(fd.DataType);
            _col.AllowSorting = true;
            if (fd.DataType.Equals("System.DateTime"))
                _col.DataFormatString = "{0:dd/MM/yyyy}";
            

        }
    if (!IsPostBack)
        SortGird(sortField, sortDir);
    }


    private void SortGird(string expression, string direction)
    {
        GridSortExpression sort = new GridSortExpression();
        sort.FieldName = expression;
        sort.SortOrder = (direction == string.Empty || direction == "asc") ? GridSortOrder.Ascending
            : GridSortOrder.Descending;
        FilesGrid.MasterTableView.SortExpressions.Add(sort);
        State.FileOrderDir = direction == string.Empty ? "asc" : direction;
        State.FileOrderField = expression;
    }

    

    protected void FilesGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        if (State.SearchGroup == QuickSearchGroup.loan)
            FilesGrid.DataSource = s.GetFilesLoans(State);
        else
            FilesGrid.DataSource = s.GetFiles(State);

    }
    protected void FilesGrid_PageIndexChanged(object source, GridPageChangedEventArgs e)
    {
        FilesGrid.CurrentPageIndex = e.NewPageIndex;
    }


    protected void FilesGrid_SortCommand(object source, GridSortCommandEventArgs e)
    {
        State.FileOrderDir = e.NewSortOrder == GridSortOrder.Descending ? "desc" : "asc";
        State.FileOrderField = e.SortExpression;

        if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
        {
            GridSortExpression sortExpr = new GridSortExpression();
            sortExpr.FieldName = e.SortExpression;// StandardFields.GetnHibernatePropertyName(e.SortExpression);
            sortExpr.SortOrder = e.NewSortOrder;

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


    }

    protected void FilesGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            object status = dataItem["BorrowStatusString"].Text;
            (dataItem["fileimagestatus"].Controls[0] as ImageButton).ImageUrl = string.Format("~/images/Borrow{0}.bmp", status);
        }

    }
}

3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 02 Nov 2010, 01:53 PM
Hello Kieran,

Note that when the grid structure is defined on the Page_Load event you should create it only on initial load. This help topic section provides more information on the matter.

To check whether the problems are Nhibernate related, I would suggest that you try binding your grid to a normal data set instead. Since the grid is bound through Advanced data binding (NeedDataSource event) the paging and sorting commands should be handled automatically. Based on that I would recommend that you comment the code in the FilesGrid_PageIndexChanged and FilesGrid_SortCommand event handlers and verify whether the grid behaves as expected.

I hope this helps

All the best,
Martin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kieran
Top achievements
Rank 1
answered on 03 Nov 2010, 01:57 AM
Hi

I tried all that and it didn't resolve anything. I have altered to datasource to now be a table retrieved directly from the database, again this didn't resolve anything.Then i moved my content page snippet out of the content page and dropped it into a page on it's own and it was fine. It appears to be fact that that the grid is in a content page in a master page. The master page contains some user controls containing telerik controls. It also - naturally - contains the RadScriptManager, also a RadAjaxManager
When I then quickly created a a page which basically denormalized the master and content parts into one stand alone page it all worked fine - so there must be some issue with the master - content page scenario. Is there anything special required or anything that I should be aware off?

Thanks
Kieran

0
Kieran
Top achievements
Rank 1
answered on 03 Nov 2010, 10:45 PM
Resolved this issue, the code in the code init that called a method on the master page caused the issue

protected void Page_Init(object sender, EventArgs e)
    {
        State.UseState = true;

        Master.SetPage();

    }

Once this was moved to page_load the grid worked perfectly

thanks
Kieran
Tags
Grid
Asked by
Kieran
Top achievements
Rank 1
Answers by
Martin
Telerik team
Kieran
Top achievements
Rank 1
Share this question
or