Friday, August 28, 2015

footer!

I was going to solve this problem this way:

$(function() {
   var footer = $("#MyFooter").find('tbody:first').html();
   var recordsContainer = $("#MyGrid").find('tbody:first').find('tbody:first');
   recordsContainer.append(footer);
}

 
 

I would try to understand ASPxGridView.ProcessColumnAutoFilter Event in the name of refreshing the footer row after a search at the DevExpress ASPxGridView column headers, but my boss pointed out to me that my JavaScript hack was... a hack. We found a better way:

<dx:ASPxGridView ID="Foo" runat="server">
   <Columns>
      <dx:GridViewDataColumn FieldName="TeaName" VisibleIndex="1" />
      <dx:GridViewDataTextColumn FieldName="PriceOfTeaInChina" VisibleIndex="2">
         <PropertiesTextEdit DisplayFormatString="${0}" />
      </dx:GridViewDataTextColumn>
   </Columns>
   <Settings ShowFooter="True" />
   <TotalSummary>
      <dx:ASPxSummaryItem FieldName="TeaName" SummaryType="Custom" />
      <dx:ASPxSummaryItem FieldName="PriceOfTeaInChina" SummaryType="Sum" />
   </TotalSummary>
</dx:ASPxGridView>

 
 

Average, Count, Max, Min, and None are the other options for SummaryType in this approach and the Custom summary type really needs some love to get it to work. On the C# side you need to wire up an event like so:

Foo.CustomSummaryCalculate += Bar;

 
 

The event itself might look like...

protected void Bar(object sender, DevExpress.Data.CustomSummaryEventArgs e)
{
   switch (((ASPxSummaryItem)e.Item).FieldName)
   {
      case "TeaName":
         e.TotalValue = _data.Select(x => x.TeaName).Distinct().Count();
         break;
   }
}

 
 

Addendum 9/15/2015: The switch statement immediately above should really be wrapped in:

if (e.SummaryProcess == CustomSummaryProcess.Finalize)
{
   
//switch goes here
}

 
 

...as otherwise the switch statement will run for each row in the ASPxGridView and create unneeded memory overhead.

No comments:

Post a Comment