Select to view content in your preferred language

How to bind a Textblock.Text to the Sum of Datagrid Column.

9417
13
05-11-2011 05:58 AM
MattMiley
Deactivated User
I have a query where I filter out Data that populates a datagrid and I need to bind a textblock.Text to the sum of the column "Valves_Turned"
<sdk:DataGrid x:Name="QueryDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="Column" Background="White"
           IsReadOnly="True"
           HorizontalScrollBarVisibility="Hidden" Grid.RowSpan="2" Margin="0,-6,0,0">
           <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Width="150" Binding="{Binding Attributes[Date]}" Header="Date" CanUserSort="True" SortMemberPath=""/>
            <sdk:DataGridTextColumn Width="70" Binding="{Binding Attributes[Valves_Turned]}" Header="Quantity"/>
            <sdk:DataGridTextColumn Width="462" Binding="{Binding Attributes[Turned_Valve_Numbers]}" Header="Turned Valve Numbers"/>
           </sdk:DataGrid.Columns>
          </sdk:DataGrid>


Valves_Turned doesn't have any null values, its a domain with the value defaulted at 0 and has the ability to go up to 25.

I just want my textblock.Text to show the sum off all the values in that column.

I tried this

text="{Binding Attributes[Valves_Turned].SUM}"


and it doesn't work.
0 Kudos
13 Replies
DominiqueBroux
Esri Frequent Contributor
Sorry dbroux

Originally Posted by dbroux
[INDENT]Try :
int Sum = featureSet.Features.Select(g => (int)g.Attributes["Valves_Turned"]).Sum();



[/INDENT]Does not work.


Difficult to guess what is not working. What are you observing?
- the sum is null
- the sum is not the right one
- it's crashing
- it's not compiling
- the sum is good but it's not reported in the TextBlock
- something else?


I double checked by adding this code to the spatialquery interactive sample :
int sum = featureSet.Features.Select(g => (int)g.Attributes["POP2000"]).Sum();

The sum looks the right one.
0 Kudos
MattMiley
Deactivated User
Difficult to guess what is not working. What are you observing?
- the sum is null
- the sum is not the right one
- it's crashing
- it's not compiling
- the sum is good but it's not reported in the TextBlock
- something else?


I double checked by adding this code to the spatialquery interactive sample :
int sum = featureSet.Features.Select(g => (int)g.Attributes["POP2000"]).Sum();

The sum looks the right one.



I also got it to work using the sample.

I think the REST Service might be the problem..

Valves_Turned (Type: esriFieldTypeString, Alias: Valves Turned, Length: 5, Domain: Coded Values: [0: 0], [1: 1], [2: 2], ...23 more... )

It probably needs to look like this

Valves_Turned (Type: esriFieldTypeInteger, Alias: Valves Turned, Length: 5, Domain: Coded Values: [0: 0], [1: 1], [2: 2], ...23 more... )


The sum results textblock doesn't do anything when I Changed the code to..

int sum = featureSet.Features.Select(g => (int)g.Attributes["STATE_NAME"]).Sum();

The sum results textblock didn't do anything, no null or nothing, it behaved just like mine.

Do you think this is the problem???
0 Kudos
JenniferNery
Esri Regular Contributor
If the field type is a string, you need to convert this to a number first before you can get the sum.

Try:
 int sum = featureSet.Features.Select(g => int.Parse((string)g.Attributes["Valves_Turned"])).Sum();
0 Kudos
MattMiley
Deactivated User
Thanks jenniferdnery & dbroux you helped me learn a lot.
0 Kudos