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

Create Multicolumn Headers programmatically

1 Answer 455 Views
Grid
This is a migrated thread and some comments may be shown as answers.
EssMus
Top achievements
Rank 1
EssMus asked on 08 Apr 2015, 08:24 AM

Hi,

I'm a new to using telerik i want to associate my column to one header text 

in the first pic i have 2 colomn ID STATION adn TIME TAG

i want have                            

                              STATION XXX

              ID STATION              TIME TAG

and in the seconde pic i have create 2 column programmatically 

plz help me

Regards,

Essoufi

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Apr 2015, 02:16 PM
Hello Essoufi,

For creating a multi column headers programmatically you need to add GridColumnGroups to the MasterTableView's ColumnGroups collection. Afterwords, you need to set the ColumnGroupName property of the columns to match the Name property of the created GridColumnGroups:
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />

And the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
    RadGrid RadGrid1 = new RadGrid();
    RadGrid1.ID = "RadGrid1";
    PlaceHolder1.Controls.Add(RadGrid1);
    RadGrid1.Width = Unit.Pixel(300);
    //Add RadGrid to the Controls collection of the placeholder
 
    if (!IsPostBack)
    {
        RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
        RadGrid1.MasterTableView.AutoGenerateColumns = false;
        RadGrid1.MasterTableView.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
 
        GridColumnGroup columnGroup = new GridColumnGroup();
        RadGrid1.MasterTableView.ColumnGroups.Add(columnGroup);
        columnGroup.HeaderText = "Station XXX";
        columnGroup.Name = "StationGroup";
 
        GridBoundColumn boundColumn;
        boundColumn = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = "ID";
        boundColumn.HeaderText = "ID";
        boundColumn.ColumnGroupName = "StationGroup";
 
        GridDateTimeColumn dateColumn = new GridDateTimeColumn();
        RadGrid1.MasterTableView.Columns.Add(dateColumn);
        dateColumn.DataField = "Date";
        dateColumn.HeaderText = "Date";
        dateColumn.ColumnGroupName = "StationGroup";
    }
}
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, DateTime.Now.AddDays(i));
    }
 
    (sender as RadGrid).DataSource = table;
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Grid
Asked by
EssMus
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or