how to add sum of values in datagrid with grouping WPF c# -


hello i'm weary beginning in coding , first serious application. have data grid grouping , want add in each group sum of values column make https://leeontech.files.wordpress.com/2010/02/final.png

i have try use many solutions internet nothing work me:

my xml

 <datagrid x:name="gridraport" canuseraddrows="false" verticalalignment="stretch" minwidth="500" alternatingrowbackground="lightblue"  alternationcount="2" margin="20,20,20,20">                     <datagrid.groupstyle>                         <groupstyle>                             <groupstyle.containerstyle>                                 <style targettype="{x:type groupitem}">                                     <setter property="template">                                         <setter.value>                                             <controltemplate targettype="{x:type groupitem}">                                                 <stackpanel >                                                     <stackpanel orientation="horizontal">                                                         <textblock margin="10,10,10,10" text="{binding name}" fontweight="bold" />                                                         <textblock  margin="30,10,10,10"  text="{binding itemcount, stringformat=liczba wycieczek: {0}}" fontweight="bold"  />                                                      </stackpanel>                                                     <itemspresenter />                                                 </stackpanel>                                             </controltemplate>                                         </setter.value>                                     </setter>                                 </style>                             </groupstyle.containerstyle>                         </groupstyle>                     </datagrid.groupstyle>                 </datagrid> 

and code behind

private void fillgridraport()     {          string cmdstring = string.empty;         using (sqlconnection con = new sqlconnection(constring))         {             cmdstring = "long query";             sqlcommand cmd = new sqlcommand(cmdstring, con);             sqldataadapter sda = new sqldataadapter(cmd);             datatable dt = new datatable("wycieczki");             sda.fill(dt);             dataview dataview = dt.asdataview();             bindinglistcollectionview cv = collectionviewsource.getdefaultview(dataview) bindinglistcollectionview;             propertygroupdescription groupdescription1 = new propertygroupdescription();             groupdescription1.propertyname = "pracownik";             cv.groupdescriptions.add(groupdescription1);             gridraport.itemssource = cv;     }     } 

i weary grateful

to sum, need converter. make class implements ivalueconverter, add resource key , reference in xaml.

here example converter, have setup take field name converterparameter.

public class sumvalues : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         var cvg = value collectionviewgroup;         var field = parameter string;         if (cvg == null || field == null)             return null;          // double field         return cvg.items.sum(r => (double)(r datarowview)[field]);         // or, if field nullable         //return cvg.items.sum(r => (r datarowview)[field] double?); // "as" can return null have use "double?"          // more complex example - string field needs converted long         //return cvg.items.sum(r => long.parse((r datarowview)[field].tostring()));     }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

add converter resource, either in app.xaml or locally datagrid:

<datagrid.resources>     <local:sumvalues x:key="sumvalues" /> </datagrid.resources> 

and, finally, add textblock groupitem's controltemplate:

<textblock  margin="60,10,10,10"  text="{binding stringformat=sum: {0}, converter={staticresource sumvalues}, converterparameter=mydatasetfieldname}" fontweight="bold"  /> 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -