0

I am new in Silverlight MVVM, I am creating a project where I am binding a data in to DataGrid.

Here is my database structure:

  • tblAuthorizationVarification (AuthorizationVarificationid, AuthorizationRequestid, number)

  • tblAuthorizationRequest (AuthorizationRequestid, name)

  • tblAuthorizationVarificationDetails (DetailId, AuthorizationRequestid, Amount)

I want to show Total of Amount in gridview for all authorization.

Below is my code, in ViewModel class, I'm getting tblAuthorizationRequest from tblAuthorizationVarification:

PagedCollectionView _AuthorizationVarificationList;

public PagedCollectionView AuthorizationVarificationList
{
  get { return _AuthorizationVarificationList; }
  set 
  {
     _AuthorizationVarificationList = value;
     OnPropertyChanged("AuthorizationVarificationList"); }
  }

  private void GetVarifications()
  {
    IsBusy = true;
    LoadOperation<AuthorizationVerification> loadOp = 
                          objContext.Load(objContext.GetCreditNotesQuery());

    loadOp.Completed += (sender, e) =>
    {
       IEnumerable<AuthorizationVerification> op = 
               ((LoadOperation<AuthorizationVerification>)sender).Entities;
       PagedCollectionView view = new PagedCollectionView(op);
       this.AuthorizationVarificationList = view;
       cnt = cnt - 1;
       if (cnt <= 0)
         IsBusy = false;
    };
  }

AuthorizationVarificationList is binding in Gridview as like

<sdk:DataGrid x:Name="grdCreditNotes" 
    ItemsSource="{Binding Path=AuthorizationVarificationList}" 
    SelectedItem="{Binding Path=SelectedCreditNote, Mode=TwoWay}" 
    AutoGenerateColumns="False" IsReadOnly="True" Grid.Row="2" 
    VerticalAlignment="Stretch" Margin="0,0,0,0">

    <sdk:DataGrid.Columns>
      <sdk:DataGridTextColumn Header="Credit No" 
         Binding="{Binding Path=AuthorizationVerificationId}" Width="200"/>

      <sdk:DataGridTextColumn Header="Amount" 
         Binding="{Binding Path=AuthorizationRequest.Amount}" MinWidth="100" 
         Width="*"/>

    </sdk:DataGrid.Columns>    
</sdk:DataGrid>

What can I do for display sum of amount of particular Authorization in this field of grid?

Jedidja
  • 16,610
  • 17
  • 73
  • 112
Viral Sarvaiya
  • 781
  • 2
  • 9
  • 29
  • @Tim Post thanks, can u please give me answer.. its urgent for me... – Viral Sarvaiya Nov 22 '11 at 07:18
  • You are on StackOverflow already for 1 year, could you please format your question yourself? It is really hard to read and understand. Especially which proeprties is exposed by ViewModel and what is `Particular Auth` – sll Nov 22 '11 at 15:24
  • @sll : Can u give me answer of my question if u understood my question, its very urgent for me to get solution of the problem. and thanks for the notifying me for my mistake. – Viral Sarvaiya Nov 22 '11 at 20:15
  • All are only editing my question, please answer me too... – Viral Sarvaiya Nov 23 '11 at 09:33
  • What you want to display? Value of the `AuthorizationRequest.Amount`? I do not see `AuthorizationRequest` in the ViewModel, where from it comes up? – sll Nov 23 '11 at 09:54
  • from AuthorizationVarification.AuthorizationRequest it will come. i have use that include("AuthorizationRequest") in AuthorizationVarification metadata, so we can also use AuthorizationVarification.AuthorizationRequest.AuthorizationVarificationDetails like that. – Viral Sarvaiya Nov 23 '11 at 10:22
  • Anyway you can expose `public int TotalAmount` proeprty and calculate it after the data is loaded, then just bind it in XAML like ``. if you need to calculate TotalAmount for each grid row - for me is not clear which values you need to sum? Each row is bound to the instance of the `AuthorizationVerification`, right? And what you need to sum per each row? – sll Nov 23 '11 at 10:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5267/discussion-between-viral-sarvaiya-and-sll) – Viral Sarvaiya Nov 23 '11 at 10:44
  • Create a new view model class as a wrapper to the existing `AuthorizationVerification` class and then you can add any properties that you want. – vortexwolf Nov 23 '11 at 11:19
  • @vorrtex can u please elaborate that?? – Viral Sarvaiya Nov 23 '11 at 12:56

2 Answers2

1

As I've already suggested, you can create a view model class for the collection item and populate it as it should be.

public class VerificationViewModel
{
    public int AuthorizationVerificationId { get; set; }

    public double Amount { get; set; }
}

Then use the LINQ-query which groups the collection and returns summarized items:

loadOp.Completed += (sender, e) =>
{
   IEnumerable<AuthorizationVerification> op = 
           ((LoadOperation<AuthorizationVerification>)sender).Entities;

   var models = op.GroupBy(item => item.AuthorizationVerificationId)
                  .Select(g => new VerificationViewModel
                                { 
                                    AuthorizationVerificationId = g.Key, 
                                    Amount = g.Sum(gi => gi.Amount) 
                                })
                  .ToList();

   PagedCollectionView view = new PagedCollectionView(models);
   // Everything else is the same
}

//Also change the type of the property which is bound to SelectedItem
public VerificationViewModel SelectedCreditNote { get; set; }

And change the binding path of the second column:

<sdk:DataGrid.Columns>
  <sdk:DataGridTextColumn Header="Credit No" 
     Binding="{Binding Path=AuthorizationVerificationId}" Width="200"/>

  <sdk:DataGridTextColumn Header="Amount" 
     Binding="{Binding Path=Amount}" MinWidth="100" Width="*"/>

</sdk:DataGrid.Columns>   

This code should calculate the sum of the Amount for each Id. If you want some other aggregation, you can change the linq query.

vortexwolf
  • 13,967
  • 2
  • 54
  • 72
  • `gi => gi.Amount` directly i can not do that, i get list of **AuthorizationVarificationList** and from that i have to get **AuthorizationRequest** and then from it i get object of **AuthorizationVarificationDetails** and then i have to do sum of amount of that. For that i write `Amount = Convert.ToDouble(g.Sum(gi=>gi.AuthorizationRequest.AuthorizationMaintenanceDetails.Sum(a=>a.AuthorizationAmount)))` but it cant come perfact amount. hope u understood well my problem now. – Viral Sarvaiya Nov 24 '11 at 06:43
  • @ViralSarvaiya Maybe you haven't loaded all foreign key relations, I think that the issue is in the `gi.AuthorizationRequest.AuthorizationMaintenanceDetai‌​ls` line. Use the `Include` function like it was described here: http://stackoverflow.com/questions/5332163/entity-framwork-ria-services-include-not-working, it must help. – vortexwolf Nov 24 '11 at 10:40
0

If I understand correctly you are after a Summary Row? If so, please check out the following link http://leeontech.wordpress.com/2010/02/01/summary-row-in-datagrid/. You will may need to shape your Model objects behind ViewModels a little better (It is rarely good practice to show Models (DTOs in this case) directly to the View).

Agies
  • 1,105
  • 8
  • 12