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

[Solved] How to define column aggregates when using load GridColumnSettings feature

3 Answers 168 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.
Tom
Top achievements
Rank 1
Tom asked on 23 Sep 2011, 12:06 AM
Hi,

We are in the process of evaluating your MVC product and ran into some trouble dealing with grouping/aggregating data.  Our use case calls for us to bind the grid to a DataTabe, but we have no design time knowledge of what columns appear in the table and need to dynamically choose which columns should be displayed in the grid (can this be done with the Bound() feature?).  We had no trouble up until the point at which we tried to enable group/aggregation.  Using our meta data (stored per user) we want to be able to dynamically assign aggregation settings to a column.  I have been unable to figure determine how to achieve this.  Do you have any suggestions? Do you support this?

I have embedded a sample of my code from my view to help paint the picture of how we are setting up our grid.  We are leveraging the LoadSettings( ) method to loading a enumerable of  GridColumnSettings.

var gSettings = Html.Telerik( ).Grid( Model.ActiveDataSet )
           .Name( "gvwYetiTasks" )
           .DataBinding( dataBinding => dataBinding.Ajax( ).Select( "AjaxBinding", "Telerik", new {id = Model.ActiveDataPanel.ID.GetValueOrDefault( )} ) )
           .Pageable( p => p.Enabled( true ).PageOnScroll( true ).PageSize( 50 ) )
           .Sortable( s => s.Enabled( true ).SortMode( GridSortMode.MultipleColumn ) )
           .Scrollable( s => s.Enabled( true ) )
           .Groupable( g => g.Enabled( true ).Visible( true ) )
           .Reorderable( r => r.Columns( true ) )
           .Resizable( s => s.Columns( true ) )
           .Selectable( s => s.Enabled( true ) )
           .Filterable( );
 
       IList<GridColumnSettings> colSettings = new List<GridColumnSettings>( );
       foreach ( var f in Model.ActiveDataPanelView.UserGridSetting.Fields.Where( vf => vf.VisibleIndex >= 0 ).OrderBy( vf => vf.VisibleIndex ) )
       {
           var dpf = Model.ActiveDataPanel.GetFieldByFieldName( f.FieldName );
           var colStg = new GridColumnSettings {Title = f.DisplayName, Visible = ( f.VisibleIndex >= 0 ), Sortable = true, Member = f.FieldName, Groupable = true, Width = "200px;", Format = dpf.Format, Filterable = true};
            
           if ( dpf.SummaryItemType != SummaryItemType.Undefined && dpf.SummaryItemType != SummaryItemType.None && dpf.SummaryItemType != SummaryItemType.WeightedAverage )
           {
               //want to set summary item data here
           }
            
           colSettings.Add( colStg );
            
            
       }
 
       gSettings.Columns( cols => cols.LoadSettings( colSettings ) );
       gSettings.Render( );


Thanks for your time and we look forward to your response.

3 Answers, 1 is accepted

Sort by
0
Sajid
Top achievements
Rank 1
answered on 17 Dec 2012, 05:59 AM
I have the same problem and do not see any aggregate result with GridcolumnSettings , here is my code snippet .Please help!

 var columnSettings = new List<GridColumnSettings>()
            {
                  new GridColumnSettings
                {
                    Member = "LastName",
                    Width = "200px",                 
                    
                },
                new GridColumnSettings
                {
                    Member = "FirstName",
                    Width = "100px",

                },
                 new GridColumnSettings
                {
                    Member = "Salary",
                    Width = "100px",
                    Format="{0}",
                    
                    FooterTemplate = (result =>  result.Sum.Format("Sum:{0}")),                  
                    FooterHtmlAttributes ={{"class","content-right" }},
          
                  
                }
          
            };
0
Sajid
Top achievements
Rank 1
answered on 18 Dec 2012, 07:14 AM
Here is the workaround 
                  
@( Html.Telerik().Grid(Model.Users)
        .Name("Grid")
        .Columns(columns => {
            columns.GenerateCustomColumns(columnSettings);          
           })
               .DataBinding(dataBinding => dataBinding.Ajax().Select("_getusers", "home"))

               .Scrollable(scrolling => scrolling.Enabled(true).Height("auto"))
               .Pageable(paging => paging.Enabled(true)
               .PageSize(10, new int[] { 5, 10, 20, 50, 100, 500 })
               .Position(GridPagerPosition.Both)
               .Total(Model.Users.Count)
               .Style(GridPagerStyles.PageSizeDropDown | GridPagerStyles.NextPreviousAndNumeric)
                               .PageTo(1))
               .Filterable(filtering => filtering.Enabled(true))
               .Reorderable(reordering => reordering.Columns(true))
                   .NoRecordsTemplate(" ")
               .EnableCustomBinding(true)
   )

// Extension method to genarate columns dynamically

public static class TelerikMvcGridColumnHelper
    {
        public static void GenerateCustomColumns<T>(this GridColumnFactory<T> columns,List<GridCustomColumnSettings> settings) where T:class
        {
            if (settings != null)
            {
                settings.ForEach(column =>
                {
                    var boundedColumn = columns.Bound(column.Member);
                    if (column.ClientFooterTemplate != null)
                        boundedColumn.ClientFooterTemplate(column.ClientFooterTemplate);

                    if (!string.IsNullOrEmpty(column.Width))
                        boundedColumn.Width(column.Width);

                });
            }
               
        }
    }


// Column settings class

  public class GridCustomColumnSettings : GridColumnSettings
     {
        public string ClientFooterTemplate { get; set; }

     }





0
Deepika
Top achievements
Rank 1
answered on 08 Jun 2013, 12:07 PM
Hi,

I have the similar requirement of adding footertemplate with columnsettings from controller during runtime.
As we have more than 20 columns in telerik mvc grid.

Please can you provide sample how you had done this using model class if it is possible.

Thanks,
Deepika
Tags
Grid
Asked by
Tom
Top achievements
Rank 1
Answers by
Sajid
Top achievements
Rank 1
Deepika
Top achievements
Rank 1
Share this question
or