This is a migrated thread and some comments may be shown as answers.

[Solved] Grid with ViewModel and List item

11 Answers 249 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gabe Calabria
Top achievements
Rank 1
Gabe Calabria asked on 19 Nov 2009, 02:38 PM
I have a view model that is similar to the following...

  public class ManageCustomDiscountsViewData : ViewModel 
  { 
        public IEnumerable<CustomerItem> CustomerSummary { getset; } 
  } 

The CustomerItem is as follows:

public class CustomerItem 
   public int CustomerID { getset; } 
   public string CustomerName { getset; } 
   public string Company { getset; } 
   public IEnumerable<AdjustmentItem> Adjustments { getset; } 

The AdjustmentItem is as follows:
public class AdjustmentItem 
   public int ID { getset; } 
   public string FormattedAmount { getset; } 
   public string Amount { getset; } 


What I want to do is use the Model.CustomerSummary to populate my grid. Well thats no problem. But I want to add a column for
each of the Adjustments, for each CustomerSummary will have the same number. So heres what I have so far...(i know it doesnt compile and is not correct due to my foreach loop for the Adjustments... but i was trying to figure out a way to explain it... Let me know what I can do to solve this! thanks so much!

<% Html.Telerik().Grid<CustomerItem>(Model.CustomerSummary) 
                            .Name("customerGrid").PrefixUrlParameters(false
                            .AssetKey("gridAssetGroup"
                            .Columns(columns => 
                            { 
                                columns.Add(c => 
                                { 
                                %> 
                                    <%= Html.CheckBox(c.CustomerID) %> 
                                <% 
                                }).Title(" ").Width(50).HtmlAttributes(new { align = "center" }); 
                                columns.Add(c => c.CustomerID).Width(50); 
                                columns.Add(c => c.Company).Width(50); 
 
                                // need to iterate thru the Adjustments for this item and add a column for each   
                                ????foreach (var item in c.Adjustments ??? ) 
                                { 
                                    columns.Add(item.Amount).Width(50); 
                                } 
                            }) 
                            .Sortable(sort => sort.SortMode(GridSortMode.SingleColumn)) 
                            .Scrollable(scrolling => scrolling.Height("100%")) 
                            .Render(); 
                        %> 




11 Answers, 1 is accepted

Sort by
0
Alex Gyoshev
Telerik team
answered on 19 Nov 2009, 03:32 PM
Hi Gabe,

Having a variable number of columns is rather strange - suppose we have the following data:

Customer { Name = "Alex", Adjustments = { "One", "Two", "Three" } }
Customer { Name = "George", Adjustments = { "One" } }

How many columns should the grid have? What will show the rows that have less data? How will the columns be named? (drawing the result is the best way to architect it)

That said, a much better option would be to make every row a customer, with the option to expand each customer and view his Adjustments. This is what we call hierarchical binding - yet we don't support such functionality yet. For an example of how you might tackle this problem, please refer to the related grids online demo - it shows Customers, and when you select a row, the Orders for the given customer get displayed in the second grid. We have the hierarchical binding functionality scheduled for the official release of the Extensions.

Best wishes,
Alex
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Gabe Calabria
Top achievements
Rank 1
answered on 19 Nov 2009, 03:38 PM
It is guaranteed that each customer will have the exact  same number of adjustments....
Is there a way to do it knowing this ? I dont want to have to navigate away from the grid to see the adjustments though, its
a general summary. Any ideas ?
0
Gabe Calabria
Top achievements
Rank 1
answered on 19 Nov 2009, 04:19 PM
Why does this blow up with a for loop inside?

I made the Adjusments a List now so i could just iterate with for loop.

public class CustomerItem 
   public int CustomerID { getset; } 
   public string CustomerName { getset; } 
   public string Company { getset; } 
   public List<AdjustmentItem> Adjustments { getset; }  // changed to a list for use with the for loop



<% Html.Telerik().Grid<CustomerItem>(Model.CustomerSummary) 
                            .Name("customerGrid").PrefixUrlParameters(false
                            .AssetKey("gridAssetGroup"
                            .Columns(columns => 
                            { 
                                columns.Add(c => 
                                { 
                                %> 
                                    <%= Html.CheckBox(c.CustomerID) %> 
                                <% 
                                }).Title(" ").Width(50).HtmlAttributes(new { align = "center" }); 
                                columns.Add(c => c.CustomerID).Width(50); 
                                columns.Add(c => c.Company).Width(50); 
 
                                 // this blows up saying the index is out of range
                                for (int i =0; i < Model.Adjustment.Count; i++
                                { 
                                    columns.Add(c => c.Adjustment[i].Amount); 
                                }
                               // replacing the for loop with this works....
                               columns.Add(c => c.Adjustment[0].Amount);
                               columns.Add(c => c.Adjustment[1].Amount);
                               columns.Add(c => c.Adjustment[2].Amount);

columns.Add(c => c.Adjustment[i].Amount);


                            }) 
                            .Sortable(sort => sort.SortMode(GridSortMode.SingleColumn)) 
                            .Scrollable(scrolling => scrolling.Height("100%")) 
                            .Render(); 
                        %> 

0
Accepted
Alex Gyoshev
Telerik team
answered on 20 Nov 2009, 09:59 AM
Hello Gabe,

A loop inside the expression is ok. The value of the index, i, however, gets calculated after the loop has finished - and therefore, the index out of bounds exception.

You should use something like this:

Action<int> addAdjustmentColumn =
    (i) => { columns.Add(c => c.Adjustment[i].Amount); };
for (int i = 0; i < Model.Adjustment.Count; i++ )  {
    addAdjustmentColumn(i);
}


This way, the expression will be compiled and can be used within the loop.

Kind regards,
Alex
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Gabe Calabria
Top achievements
Rank 1
answered on 20 Nov 2009, 04:44 PM
Thank you very much that worked perfectly.
0
Radoslav Semerdjiev
Top achievements
Rank 1
answered on 04 May 2010, 01:50 PM
Hi Guys,

I have the same problem, only I need to be able to display the column captions for the respective to Adjustment collection.

In my case the column captions are dates and for each one I must display the numeric value, which is held in the collection

Many thanks,

Radoslav
0
Alex Gyoshev
Telerik team
answered on 04 May 2010, 02:34 PM
Hello Radoslav,

Could you please elaborate the problem with a more specific example (i.e. with a bit of code and the expected result)?

Thanks,
Alex Gyoshev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Radoslav Semerdjiev
Top achievements
Rank 1
answered on 04 May 2010, 03:18 PM
OK, so I have a three-level object hierarchy, such that:


    public interface IReferenceRate 
    { 
        string Code { get; set; } 
        int Frequency { get; set; } 
 
        IList<IFixing> Fixings { get; } 
    } 
 
    public interface IFixing  
    { 
        DateTime InitialFixingDate { get; set; } 
        string Reference { get; set; } 
         
        IList<IRate> Rates { get; } 
    } 
 
    public interface IRate  
    { 
        DateTime ForwardDate { get; set; } 
        double Value { get; set; } 
    } 
 

I map the first and second level objects with the following two:


    public interface IReferenceRateFixingView 
    { 
        string Reference { get; set; } 
        string Frequency { get; set; } 
        string ReferenceRate { get; set; } 
        string InitialFixingDate { get; set; } 
 
        IList<IRateValueView> RateValues { get; set; } 
    } 
 
    public interface IRateValueView 
    { 
        string Value { get; set; } 
    } 
 

    
    I aim to display a grid with the  following layout:
    
    Reference | Frequency | Reference Rate | Initial Fixing Date | 01-Jan-2010 | 02-Jan-2010 | 03-Jan-2010 | etc.
    
    Please note ultimately I aim to derive the column captions for the dates (e.g. "01-Jan-2010" etc.) from the IRate.ForwardDate property. For this purpose I map it with its respective view class:

    public interface IRateForwardDateView 
    { 
        string ForwardDate { get;} 
    } 
 


So far I have tried this...

  
 <% Html.Telerik().Grid((IList<IReferenceRateFixingView>)ViewData["ReferenceRates"])  
        .Name("referenceRates").PrefixUrlParameters(false)  
        .Columns(columns =>  
        {  
            columns.Add(c => c.Reference).Width(200);  
            columns.Add(c => c.Frequency).Width(200); 
            columns.Add(c => c.ReferenceRate).Width(200); 
            columns.Add(c => c.InitialFixingDate).Width(200); 
     
            Action<int> addForwardDateColumn = (i) => {columns.Add(c => c.RateValues[i].Value); }; 
     
            int ratesSize = ((IList<IReferenceRateFixingView>) ViewData["ReferenceRates"])[0].RateValues.Count; 
            for (int i = 0; i < ratesSize; i++) 
            { 
                addForwardDateColumn(i); 
            } 
        }) 
        .Pageable(paging => paging.PageSize(10).Style(GridPagerStyles.NextPreviousAndInput).Position(GridPagerPosition.Bottom))                                                 
        .Render();  
    %>          
 
    
... which produces something like this:
Reference Frequency Reference Rate Initial Fixing Date Value Value Value Value Value Value
ERIBO3-3-40086 3 ERIBO3 30-Sep-09 0.74% 0.73% 0.82% 1.11% 1.40% 1.68% 1.90% 2.12%
ERIBO3-3-40087 3 ERIBO3 01-Oct-09 0.75% 0.71% 0.83% 1.11% 1.43% 1.65% 1.90% 2.11%
ERIBO3-3-40088 3 ERIBO3 02-Oct-09 0.75% 0.70% 0.83% 1.11% 1.44% 1.65% 1.90%


I don't know how to display the respective dates as column captions.

I have also tried to map this object graph to a DataTable, where you can dynamically specify the columns' captions, but still to no good.

Hope this make sense to you.

Many thanks,

Radoslav
0
Atanas Korchev
Telerik team
answered on 04 May 2010, 04:07 PM
Hello Radoslav Semerdjiev,

To set the column caption you should use the Title method. I guess this could help:
Action<int> addForwardDateColumn = (i) => {columns.Add(c => c.RateValues[i].Value).Title("1/1/2000"); };

You just need a way to extract the required value.

All the best,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Radoslav Semerdjiev
Top achievements
Rank 1
answered on 04 May 2010, 04:43 PM
Hi Atanas,

This thing you suggested did work! Many thanks (i golemi blagodarnosti :-)!!! 
0
Atanas Korchev
Telerik team
answered on 04 May 2010, 04:45 PM
Hello Radoslav Semerdjiev,

You are most welcome (за нищо ;-)!

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Gabe Calabria
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Gabe Calabria
Top achievements
Rank 1
Radoslav Semerdjiev
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or