Grouping with custom Adaptor

Hi

i have a custom grid data adaptor and everything works perfect but the grouping is not working. I couldn't find any example with grouping and tried the following: 
  if (dm.Search != null && dm.Search.Count > 0)
{
// Searching
dataSource = DataOperations.PerformSearching(dataSource, dm.Search);
}

if (dm.Sorted != null && dm.Sorted.Count > 0)
{
// Sorting
dataSource = DataOperations.PerformSorting(dataSource, dm.Sorted);
}
else
{
var properties = typeof(TTarget).GetProperties().ToList();
// for take and skip we always need one sort prop
if (properties.Any(a => a.Name == "Id"))
{
dataSource = DataOperations.PerformSorting(dataSource, new List<Sort>() {new Sort() {Name = "Id", Direction = "ascending"}});
}
else if (properties.Any())
{
dataSource = DataOperations.PerformSorting(dataSource, new List<Sort>() {new Sort() {Name = properties.First().Name, Direction = "ascending"}});
}
else
{
throw new Exception("The grid needs at least one property in the generic model.");
}
}

if (dm.Where != null && dm.Where.Count > 0)
{
// Filtering
dataSource = DataOperations.PerformFiltering(dataSource, dm.Where, dm.Where[0].Operator);
}

int count = dataSource.Count();
if (dm.Skip != 0)
{
//Paging
dataSource = DataOperations.PerformSkip(dataSource, dm.Skip);
}

if (dm.Take != 0)
{
dataSource = DataOperations.PerformTake(dataSource, dm.Take);
}

if (dm.Group != null && dm.Group.Any())
{
var test = dataSource;
var res = DataOperations.PerformGrouping<TTarget>(test, dm.Group);
return dm.RequiresCounts ? new DataResult() {Result = res, Count = count} : (object) res;
}
After the data is returned by the read function an exception is thrown in the group model generator.

GroupModelGenerator Line 39:
System.InvalidCastException: Unable to cast object of type 'Syncfusion.Blazor.Data.GroupResult' to type 'Syncfusion.Blazor.Data.Group`1[HQSB.Blazor.Web.DataModel.UserReportingDto]'.
  at at Syncfusion.Blazor.Grids.Internal.GroupModelGenerator`1.GenerateRows(IEnumerable data, Int32 startIndex)

Can you give me a hint?

Thanks in advance.




3 Replies

VN Vignesh Natarajan Syncfusion Team March 26, 2020 05:02 AM UTC

Hi Nils,  
 
Greetings from Syncfusion support  
 
Query: “i have a custom grid data adaptor and everything works perfect but the grouping is not working 
 
As per your requirement we have prepared a sample to perform Grouping in Grid with CustomAdaptor. Refer the below modified code example.  
 
public override object Read(DataManagerRequest dmstring key = null) 
        { 
  
            IEnumerable 
                DataSource = Orders; 
            DataResult DataObject = new DataResult(); 
. . . .. . . . . 
            if (dm.Skip != 0) 
            { 
                //Paging 
                DataSource = DataOperations.PerformSkip(DataSourcedm.Skip); 
            } 
            if (dm.Take != 0) 
            { 
                DataSource = DataOperations.PerformTake(DataSourcedm.Take); 
            } 
            if (dm.Group != null) 
            { 
                IEnumerable GroupData = Enumerable.Empty<object>(); 
                foreach (var group in dm.Group) 
                { 
                    DataSource = DataUtil.Group<Order>(DataSourcegroupdm.Aggregates, 0, dm.GroupByFormatter); 
                } 
                DataObject.Result = DataSource; 
                DataObject.Count = count; 
                return dm.RequiresCounts ? DataObject : (object)DataSource; 
            } 
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
        } 
 
 
Kindly download the sample which we have prepared using 18.1.0.36-beta from below  
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan. 
 



UN Unknown March 26, 2020 07:21 AM UTC

Works perfect, thank you so much.


VN Vignesh Natarajan Syncfusion Team March 26, 2020 11:18 AM UTC

Hi Nils,  

Thanks for the update.  

We are glad to hear that you have resolved your query using our solution.  

Kindly get back to us if you have further queries.  

Regards, 
Vignesh Natarajan. 


Loader.
Up arrow icon