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

How to add columns dynamically to radgrid ?

1 Answer 315 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bhanu
Top achievements
Rank 1
Bhanu asked on 23 Jan 2013, 01:20 PM
Hi,

We are using Telerik.Web.UI.dll version - 2009.3.1314.20 with visual studio 2008.
Currently our radgrid displays as below. No. of Factories are fixed and Factory names are known before the grid is configured as usual. Since we know the no. of factories and factory names (i.e. column names of the grid) well before we are using a simple DTO structure, a class with the following 5 properties and binding to the radgrid respectively - ItemName, Fac1, Fac2, Fac3, Fac4.

Item Name Factory 1   Factory 2   Factory 3   Factory 4
Item 1 12   13   14   15
Item 2 22   23   24   25
Item 3 32   33   34   35

But recently we have comeup with a new requirement that there will be a factory added on a regular basis, may be one factory per month / 2 months and we don't want to modify the code everytime when a factory is added. In this case, how can we modify the radgrid to meet this requirment ? Assuming that "Factory 5" is added next month and the output should be displayed as below. 

Item Name Factory 1   Factory 2   Factory 3   Factory 4 Factory 5
Item 1 12   13   14   15 16
Item 2 22   23   24   25 26
Item 3 32   33   34   35 36

So we don't know the no. of columns and column names during design time. I think single level DTO structure will not be sufficient. Can we use a two level DTO as below ? If yes, how can we bind a two level DTO to a grid ? How can we mention the "datafield" name while configuring a grid column using the two level DTO ?
Or can we add properties (factorynames) to a class dynamically in .NET 3.5 ?
Do we have any demo samples on this kind of behaviour ?
Any help on this is very much appreciated.

public class ItemFactoryDTO
    {
         
        [DataMember]
        public long ItemId
        { get; set; }
         
        [DataMember]
        public long ItemName
        { get; set; }
         
        [DataMember]
        public ICollection<FactoryQuantityDTO> factoryQuantityDTOs
        { get; set; }
        
    }
 
public class FactoryQuantityDTO
  {
    
    [DataMember]
    public string FactoryName { get; set; }
 
    [DataMember]
    public int? FactoryQuantity { get; set; }
 
  }


1 Answer, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 28 Jan 2013, 10:54 AM
Hello Bhanu,

In order to achieve your scenario you could populate a DataTable object from the ItemFactoryDTO object and loop through all factoryQuantityDTOs and add them as columns as shown below and set the RadGrid AutoGenerateColumns to true.
ItemFactoryDTO dto = new ItemFactoryDTO();
DataTable table = new DataTable();
foreach (FactoryQuantityDTO factory in dto.factoryQuantityDTOs)
{
    table.Columns.Add(factory.FactoryName);
}

Greetings,
Antonio Stoilkov
the Telerik team
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 RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Bhanu
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Share this question
or