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

Help Needed: Sort dynamically added GridTemplateColumn in a RadGrid

1 Answer 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Angelo
Top achievements
Rank 1
Angelo asked on 05 Feb 2014, 03:57 AM
Hi.

I have a RadGrid (see attachment - 01 - Grid.jpg). 
The first few columns (bold headers) have their columns declared declaratively.

RadGrid ID is GV_DailySales

           <telerik:GridTemplateColumn 
                    HeaderText="Gross Sales<br/>&nbsp;"
                    SortExpression="GrossSales">

and their data comes from a generic List<DailySales>. The Sort Expression value of "GrossSales"
is a property of this generic list and I am able to sort this column using the basic column sorting procedure.
This is true for the first few columns (from column "DAY" to "Average Check" because they are properties
defined in the DailySales object so no problems there. File attachment 01 - Grid.jpg shows an example
of the sorting that works fine.

--------------------------------------------------------------------------------------
For the next few columns they are of type GridTemplateColumn but had been added dynamically.

Here's the code snippet on how I added them dynamically.
           
            GV_DailySales.DataSource = ds.getDailySalesForTheMonth(DailySales_Date);  //source data for the columns that sorts fine

            #region dynamically add GridTemplateColumns for ATP

            List<PL_StoreSupport.Products> P = ds.getAllProductsForDailySales();   //my source data for the dynamic columns

            for (int i = 0; i < P.Count; i++)
            {
                TL.GridTemplateColumn gridTemplateColumn = new TL.GridTemplateColumn();
                TL.RadButton b = new TL.RadButton() { Width = Unit.Pixel(25) };
                b.Icon.PrimaryIconUrl = "~/ScriptsStylesItems/Images/icon_trends2.png";
                Label l = new Label() { Text = "<br/>" + P[i].Product_Name };
                gridTemplateColumn.HeaderTemplate = new CreateItemTemplate(b, l);
                gridTemplateColumn.HeaderStyle.Width = Unit.Pixel(85);
                gridTemplateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                gridTemplateColumn.UniqueName = Guid.NewGuid().ToString(); //or whatever name?
                //gridTemplateColumn.SortExpression = "SortExpression";//to determine which --> does not work
                GV_DailySales.MasterTableView.Columns.Add(gridTemplateColumn);
            }

            #endregion

----------------------- here's the code snippet how i defined my ITemplate----------------------------
//just a matter of adding a label and a RadButton on the header 

#region CreateItemTemplate
    public class CreateItemTemplate : ITemplate
    {
        private TL.RadButton RadButton_viewStat;
        private Label label;
        
        public CreateItemTemplate()
        {
             
        }
        public CreateItemTemplate(TL.RadButton b, Label l)
        {
            this.RadButton_viewStat = b;
            this.label = l;
        }

        public void InstantiateIn(Control container)
        {
            container.Controls.Add(RadButton_viewStat);
            container.Controls.Add(label);
        }
    }
    #endregion


----------------------- I created a Label for the dynamically created columns to hold the row data ---------------------------
 protected void GV_DailySales_ItemCreated(object sender, TL.GridItemEventArgs e)
        {
            TL.GridDataItem ITEM = e.Item as TL.GridDataItem;

            switch (e.Item.ItemType)
            {
                #region add numberic text box | asp labels

                case TL.GridItemType.Item:
                case TL.GridItemType.AlternatingItem:

                    for (int i = 9; i < ITEM.Cells.Count; i++)
                    {
                        #region RAD NUMERIC TEXT BOXES & LABELS

                        //..generic controls - dataLabel - asp labels
                        Label dataLabel  = new Label();
                        dataLabel.ID = "dataLabel_" + i.ToString();
                        dataLabel.Text = "85";
                        ITEM.Cells[i].Controls.Add(dataLabel);

                        //..adjust alignment
                        ITEM.Cells[i].HorizontalAlign = HorizontalAlign.Center;

                        #endregion

                        
                    }
                    break;

                #endregion
            }

--------------------------------- I populated the rows with random integers to simulate data ---------------------------
protected void GV_DailySales_ItemDataBound(object sender, TL.GridItemEventArgs e)
        {
            if ((e.Item.DataItem != null) && (e.Item is TL.GridDataItem))
            {
                #region DYNAMIC COLUMNS

                for (int i = 9; i < ITEM.Cells.Count; i++)
                {
                    Label dataLabel = ITEM.FindControl("dataLabel_" + i.ToString()) as Label;
                    dataLabel.Text = (i * i * ITEM.RowIndex).ToString();

                }

                #endregion
            }
        }


------ 
I did not use the the GV_DailySales.Databind() method anymore because I am already handling this via the 
GV_DailySales_NeedDataSource event handler as I've read this to be "recommended practice."

I'd like to ask for some help how I can possible sort these dynamically added columns.







1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 07 Feb 2014, 03:48 PM
Hi Angelo,

From the code I noticed that the columns are filled with random data instead of data from a field included in the grids data-source. Note that RadGrid being a bound control will perform operations like sorting and filtering based on values included in it's source. That said the behavior experienced is expected. In order to make a GridTemplateColumn sortable it should be linked to a data-field and a SortExpression for it should be defined(an example of the last mentioned is demonstrated here).

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Angelo
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or