Sorting a DataGrid

Posts   
 
    
andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 15-Jun-2006 18:38:53   

Hi

I'm evaluating LLBLGen Pro. Please bear with me as I am a bit thick and very hot.

How would i bind an entity collection to a datagrid and allow client side sorting. I using asp.net 1.1 and vb.net.

Here is my rough idea

Page_Load

  • getEntityCollection e,g, customer collection -set property that allows client side sorting = true (cant remember its name)
  • bind to datagrid

ASPX

datagrid <asp:BoundColumn DataField="Surname" HeaderText="First Name" SortExpression="Surname"/> etc

OnSort EVENT Do i have to get the entity collection again? How do i sort it? Is it something like entityColl.ApplySort(e.sortExpression)

Please can you give me some concrete code when you answer. I'm not very good at filling in the gaps. Thanks very much

andrea

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 15-Jun-2006 23:37:27   

Many times sorting a datagrid involves paging, which will mean that you will need execute another query when a different sort column is chosen. I would recommend that you setup an event handler for the SortCommand to handle setting the new sort column and the direction.

I would also setup a method that fetches the data for your grid.

You should maintain the current sort column along with the direction in ViewState or Session.

Here's a snippet of code that I have in C# that does this.

public partial class _Default : System.Web.UI.Page 
{
    private string SortColumn
    {
        get { return ViewState["SortColumn"].ToString(); }
        set { ViewState["SortColumn"] = value; }
    }

    private bool Ascending
    {
        get { return (bool)ViewState["Ascending"]; }
        set { ViewState["Ascending"] = value; }
    }

    public int PageIndex
    {
        get { return (int)ViewState["PageIndex"]; }
        set { ViewState["PageIndex"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }

    private void mAwardees_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        if (this.SortColumn == e.SortExpression)
            this.Ascending = !this.Ascending;
        else
            this.Ascending = true;

        SortColumn = e.SortExpression;
        BindData();
    }

    public void BindData()
    {

        mAwardees.DataSource = AwardeeStatusSearch.Search(SortColumn, Ascending, PageIndex + 1, mAwardees.PageSize);
        mAwardees.CurrentPageIndex = PageIndex;
        mAwardees.VirtualItemCount = AppContext.Current.AwardeeStatusSearch.RecordCount;
    }
}

andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 16-Jun-2006 13:08:24   

Hi

Thanks very much for your reply.

This is the bit I don't get:

AwardeeStatusSearch.Search(SortColumn, Ascending, PageIndex + 1, mAwardees.PageSize);

What type of object is AwardeeStatusSearch?

andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 16-Jun-2006 13:10:19   

MOre questions.

Forgot to say, where is AwardeeStatusSearch told what data to get. I can see that you are telling it to sort and page its data, but how does it know what to get in the first palce. This question might be answered already though when you tell me what type of object it is

Thanks a lot andrea

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Jun-2006 15:18:31   

AwardeeStatusSearch looks like a business class that he created and that has a method search that retrieves all records from the table AwardeeStatus with the specified sort & paging stuff.

You may use a similar approach and pass a filter with the other parameters to fetch a specific subset of the entire rows of the table.

andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 16-Jun-2006 15:45:40   

Hi

I still don't actually know how perform a sort!

Any chance of an example of how to sort using the llblgen pro classes themselves and not custom business object

TRhanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 16-Jun-2006 16:05:15   

Using the generated code -> Selfservicing / Adapter -> Filtering and sorting -> Sorting. It's in the docs.

Frans Bouma | Lead developer LLBLGen Pro