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

Custom Sorting where sort expression is different than DataField

1 Answer 251 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 21 Feb 2013, 08:36 PM
I have the following custom sort handler in a radgrid.

For most columns, this is fine.  But on occasion my datafield name is different than my object field name.

ie.  a column with the following works
col.DataField = "Id";
col.UniqueName = "Id";
col.SortExpression = "Id";

but this column fails with error:

No property or field 'OnDate' exists in type 'EvidenceEntryGridItem


col.DataField = "DateEntered";
col.UniqueName = "DateEntered";
col.SortExpression = "OnDate";

This happens after the NeedDataSourceHandler finishes, so I am successfully getting results, and getting them into the grid.  It's something internal to the grid that is failing.

protected void OnSortCommand(object sender, GridSortCommandEventArgs e)
{
    SortDirection direction = (e.NewSortOrder == GridSortOrder.Descending)
                                  ? SortDirection.Descending
                                  : SortDirection.Ascending;
    Sort = new SortBy(e.SortExpression, direction);
     
    GridSortExpression newSortExpression = new GridSortExpression();
    newSortExpression.FieldName = e.SortExpression;
    newSortExpression.SortOrder = e.NewSortOrder;
 
    MasterTableView.SortExpressions.Clear();
    MasterTableView.SortExpressions.Add(newSortExpression);
 
    e.Canceled = true;
 
    this.PageInfo = new GridPageInfo(1, this.PageSize);
    this.Rebind();
}

NeedDataSource is pretty straight forward (it is in VB)

Protected Sub ResultsGrid_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs) Handles ResultsGrid.NeedDataSource
            Dim results As EvidenceEntryGridItemResultsEnvelope = AppDomain.Instance.DI.Get(Of IEvidenceEntryGridItemRepository)().GetForUserByUniqueId(_searchValue, UserId, ResultsGrid.PageInfo, ResultsGrid.Sort, ResultsGrid.Filter)
 
            ResultsGrid.VirtualItemCount = results.TotalCount
            ResultsGrid.DataSource = results.Records
 
        End Sub


Am I missing something in my sort handler?

1 Answer, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 22 Feb 2013, 07:15 PM
I was able to code around this for the most part, by just setting the SortExpression to be the same as UniqueName and DataField, and maintaining my own dictionary of UniqueName, ActualSortTerm.  Then I just use my dictionary value for my binding query, and add the SortExpression term to the SortExpressions collection so the ui in the grid works as expected.
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Share this question
or