collection summary property - bound to windows form datagridview

collection summary property - bound to windows form datagridview

Old forum URL: forums.lhotka.net/forums/t/11119.aspx


fredg posted on Thursday, February 02, 2012

Hi,

   We have a datagridview in windows form which is bound to a collection of business object. One column(property) uses a sum of another column(property), e.g. percentage of total price:

          $% = price/sum(price).

Can we use sth like custom business rule to calculate this property? How can we get the sum(price) in the business object class?

If not, do we have other way to achieve this? 

Thanks.

RockfordLhotka replied on Thursday, February 02, 2012

You could implement a TotalPrice property in the collection object, and make sure to recalculate this property value on:

Then in the business rule in the child class, use the Parent property to access the collection where you can get the TotalPrice value.

fredg replied on Thursday, February 02, 2012

Thanks. Parent works.

Another question: If I want to add a total row into the bottom of datagridview, because datagridview is bound to bindingsource, what's the best way to achieve this? I have some ideas:

   1. insert another datagridview under primary datagridview;

   2. is there any custom datagridview with footer?

   3. Add total row into bindingsource's datasource when binding and remove it when updating?

.......

Thanks.

JonnyBee replied on Thursday, February 02, 2012

Some grids have builtin funtions for summary/total lines.

For others you may create a "fake line" with a total property shown below the grid.

I would try to not create e "fake" line in the list and try to keep in sync at bottom. 

fredg replied on Thursday, February 02, 2012

"Some grids have builtin funtions for summary/total lines."

Can you please provide some sample or link? Thanks.

 

JonnyBee replied on Friday, February 03, 2012

I'm fairly certain most commercial 3rd party grids have this feature.

C1 FlexGrid supports Range Aggregates, http://www.componentone.com/superproducts/flexgridwinforms/

DevExpress XtraGrid  support Totals and Group summary: http://devexpress.com/Products/NET/Controls/WinForms/Grid/

Infragistics WinGrid supports fomulas much like Excel. http://www.infragistics.com/dotnet/netadvantage/winforms/wingrid.aspx#Overview

just to mention a couple of grids.

vschaak replied on Friday, February 03, 2012

Hi fredg,

there are even approaches to do such summary with the standard winforms datagridview:

Sample:

http://dzaebel.net/DgvFooter.htm

HTH

Volker

Copyright (c) Marimer LLC