sorting a datagrid using e.sortExpression

Posts   
 
    
andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 20-Jun-2006 20:43:43   

Hi

I would like to know how to sort a datagrid when a user clicks on a hyperlink at the top of a datagrid column.

I have seen in the documentation on p143 that I can sort a datagrid in this way by getting the entire recordset in question and calling sort on the entity collection.

I have already done something like this as follows where viewState sortExpression stores e.sortExpression from the sortCommand event

customers.GetMulti(Nothing) customers.SupportsSorting = True customers.Sort(customers(0).Fields(ViewState("SortExpression")).FieldIndex, ComponentModel.ListSortDirection.Descending)

    dgCustomers.DataSource = customers
    dgCustomers.DataBind()

But what if i want to sort the data on the database and not on the server?

How can i convert a column name as supplied by e.sortExpression into an entity field (IENtityField) like CustomerFieldIndex.customerName so that I can make a sort clause and pass it to getMulti?

Thanks very much andrea

andieje
User
Posts: 28
Joined: 17-May-2006
# Posted on: 20-Jun-2006 21:06:00   

Hi

I got this to work

    Dim c As New CustomerEntity
    Dim field As IEntityField = c.Fields(viewstate("SortExpression"))


    Dim sorter As New SortExpression
    Dim sc As New SortClause(field, SortOperator.Descending)
    sorter.Add(sc)

    Dim customers As New CustomerCollection
    customers.GetMulti(Nothing, 0, sorter)


    dgCustomers.DataSource = customers
    dgCustomers.DataBind()

Is there a better way?

Thanks a lot andrea

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Jun-2006 08:15:09   

That's correct (no better way)