Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to sort a ASPxPivotGrid by clicking on a field value

$
0
0

The PivotGrid allows sorting by any column. To do this, right click any field value and select an appropriate field to sort by it. Sometimes it is possible to sort by any column by simply clicking on it. This example shows how to implement this behavior. You should create a template for field values to place a hyperlink (or a button) rather than a plain text.

[C#]
ASPxPivotGrid1.FieldValueTemplate=newFieldValueTemplate(ASPxPivotGrid1);

The FieldValueTemplate class should implement the ITemplate interface. We've replaced standard contents of a field value in the InstantiateIn method.

[C#]
publicclassFieldValueTemplate:ITemplate{   publicFieldValueTemplate(ASPxPivotGridpivotGrid){       this.pivotGrid=pivotGrid;   }   ASPxPivotGridpivotGrid;   protectedASPxPivotGridPivotGrid{get{returnpivotGrid;}}   #regionITemplateMembers   publicvoidInstantiateIn(Controlcontainer){       PivotGridFieldValueTemplateContainerc=(PivotGridFieldValueTemplateContainer)container;       HyperLinklink=newHyperLink();       link.Text=(string)c.Text;       link.NavigateUrl="#";       link.Attributes["onclick"]=GetOnClickHandler(c);       c.Controls.Add(link);       boolisSortedByColumn=GetIsSortedByColumn(c);       if(isSortedByColumn){           c.Controls.Add(newLiteralControl("&nbsp;*"));       }   }   boolGetIsSortedByColumn(PivotGridFieldValueTemplateContainerc){       List<PivotGridFieldPair>sortedFields=PivotGrid.Data.VisualItems.GetSortedBySummaryFields(c.ValueItem.IsColumn,c.ValueItem.Index);       boolisSortedByColumn=sortedFields!=null&&sortedFields.Count> 0;       returnisSortedByColumn;   }   stringGetOnClickHandler(PivotGridFieldValueTemplateContainerc){       StringBuilderres=newStringBuilder();       res.Append(pivotGrid.ClientInstanceName).Append(".PerformCallback('SC|");       res.Append(GetFieldIndex(c)).Append("|")           .Append(c.ValueItem.IsColumn).Append("|")           .Append(c.ValueItem.VisibleIndex).Append("|")           .Append(c.ValueItem.DataIndex);       res.Append("');");       returnres.ToString();   }   intGetFieldIndex(PivotGridFieldValueTemplateContainerc){       returnc.ValueItem.Field!=null?c.ValueItem.Field.Index:-1;   }   #endregion}

The GetOnClickHandler method returns a javascript code that will perform a ASPxPivotGrid's callback passing all required parameters to the server. On the server side, you should handle the CustomCallback event handler and apply sorting to it.

[C#]
   protectedvoidASPxPivotGrid1_CustomCallback(objectsender,PivotGridCustomCallbackEventArgse){       string[]args=e.Parameters.Split('|');       if(args[0]=="SC")           HandleSortByColumnClick((ASPxPivotGrid)sender,args);   }   voidHandleSortByColumnClick(ASPxPivotGridpivotGrid,string[]args){       intfieldIndex=int.Parse(args[1]),           visibleIndex=int.Parse(args[3]),           dataIndex=int.Parse(args[4]);       boolisColumn=bool.Parse(args[2]);       PivotAreaarea=GetArea(isColumn),           crossArea=GetCrossArea(isColumn);       PivotGridFielddataField;       List<PivotGridField>fields;       List<object>values;       GetFieldsAndValues(pivotGrid,fieldIndex,visibleIndex,dataIndex,area,           outdataField,outfields,outvalues);              SetSortByColumn(pivotGrid,crossArea,dataField,fields,values);   }

Sorting can be set by filling the SortBySummaryInfo structure. The BeginUpdate/EndUpdate method calls are necessary to prevent multiple data recalculations.

[C#]
   voidSetSortByColumn(ASPxPivotGridpivotGrid,PivotAreacrossArea,PivotGridFielddataField,List<PivotGridField>fields,List<object>values){       pivotGrid.BeginUpdate();       List<PivotGridField>crossFields=pivotGrid.GetFieldsByArea(crossArea);       for(inti= 0;i<crossFields.Count;i++){           crossFields[i].SortBySummaryInfo.Field=dataField;           crossFields[i].SortBySummaryInfo.Conditions.Clear();           for(intj= 0;j<values.Count;j++){               crossFields[i].SortBySummaryInfo.Conditions.Add(                   newPivotGridFieldSortCondition(fields[j],values[j]));           }       }       pivotGrid.EndUpdate();   }
Question Comments

Added By: conservation at: 12/23/2012 7:09:38 PM    

I did it but i don't see the view pane of pivotGrid change anything. Could you please show me how to sort as the sort way that pivotgrid used when right click in header row or column and choose Sort "xxxx" by this Column/row ....
Just some necessary functions that i can use to sort pivotgrid view pane.
Thanks

Added By: Deepika K at: 11/11/2014 4:27:37 AM    

This code didn't work for me

Added By: Maxim (DevExpress Support) at: 11/11/2014 5:52:47 AM    

Hello Deepika,
I have created a separate thread for your inquiry. 
The E1439 example does not work properly
I will answer you as soon as possible. 


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>