Best way to get sortable columns in Gridview

Posts   
 
    
Posts: 16
Joined: 11-May-2010
# Posted on: 13-May-2010 03:29:07   

If i try to bind an EntityCollection object directly to the Gridview and I try to make the columns sortable i get the following error:

The data source 'xxxxx' does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet.

So what is the best way to go around this? Convert the collection to a Datatable? Or is there any better way to go?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2010 05:25:43   
David Elizondo | LLBLGen Support Team
Posts: 16
Joined: 11-May-2010
# Posted on: 13-May-2010 06:46:40   

Thanks for that. I have sorted my problem for the time being. I really need to take a look at the LLBGen Datasource...

For the moment I am using the SortParameterName attribute of the ObjectDatasource, then using that info i build a SortExpression, that seems to works well...

Cheers

Luis



protected ISortExpression GetSortExpression<TEntity>(string sortExpression) where TEntity : EntityBase2, IEntity2, new()
        {
            ISortExpression output = null;
            if (!string.IsNullOrEmpty(sortExpression))
            {
                string[] bits = sortExpression.Split(' ');
                IEntityFieldCore field = GetFieldByColumn<TEntity>(bits[0]);

                SortOperator sort = SortOperator.Ascending;
                if (bits.Length > 1)
                {
                    sort = GetSortOperator(bits[1]);
                }
                output = new SortExpression(new SortClause(field,null, sort));
            }
            return output;
        }

        private IEntityFieldCore GetFieldByColumn<TEntity>(string fieldName) where TEntity : EntityBase2, IEntity2,new()
        {
            return new TEntity().GetFieldByName(fieldName);
        }

        private SortOperator GetSortOperator(string operatordesc)
        {
            return  (operatordesc.ToLower()=="desc")?SortOperator.Descending:SortOperator.Ascending;
        }