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

how to create dynamic headertemplates

5 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Saadettin
Top achievements
Rank 1
Saadettin asked on 30 Oct 2013, 11:13 AM
Hi. I need to create telerik grid that creates its columns automatically. Some of the columns can have mixed columns like below:

Name                 Surname                              Info
                                                        Date                   Number
------------------------------------------------------------------------------
John                  White                     4.4.2011                  45
Tracey              Dapt                       5.4.2011                  70    

I try to create a table that  its view like above. Also, I will create headers dynamically. How can I achieve this? I searched a bit of GirdTemplateColumn and ITemplate. Can anyone give me an idea?
Thanks in advance...

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Oct 2013, 11:41 AM
Hi Saadettin,

Please try the following sample code snippet to create multicolumn headers programmatically.

ASPX:
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
    SelectCommand="SELECT [OrderID], [ShipCity], [ShipName], [ShipCountry] FROM [Orders]">
</asp:SqlDataSource>

C#:
RadGrid RadGrid1 = new RadGrid();   
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = SqlDataSource1;
}
protected void Page_Init(object sender, EventArgs e)
{
    GetGrid();
}
private void GetGrid()
{
    RadGrid1.ID = "RadGrid1";
 
    GridBoundColumn cCode = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(cCode);
    cCode.HeaderText = "OrderID";
    cCode.DataField = "OrderID";
    cCode.UniqueName = "OrderID";
    cCode.ReadOnly = true;
    cCode.ItemStyle.Width = 50;
 
    GridBoundColumn cDescription = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(cDescription);
    cDescription.HeaderText = "ShipName";
    cDescription.DataField = "ShipName";
    cDescription.UniqueName = "ShipName";
    cDescription.ReadOnly = true;
    cDescription.ItemStyle.Width = 250;
 
    RadGrid1.Width = Unit.Pixel(600);
    RadGrid1.AllowPaging = true;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.Skin = "Web20";
    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
 
    GridColumnGroup columnGroup = new GridColumnGroup();
    columnGroup.Name = "SingleAction";
    columnGroup.HeaderText = "Info";
    RadGrid1.MasterTableView.ColumnGroups.Add(columnGroup);
        
        GridBoundColumn column = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(column);
        column.HeaderText = "ShipCity";
        column.UniqueName = "ShipCity";
        column.ColumnGroupName = "SingleAction";
        column.DataField = "ShipCity";
        column.ItemStyle.Width = 50;
 
        GridBoundColumn column1 = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(column1);
        column1.HeaderText = "ShipCountry";
        column1.UniqueName = "ShipCountry";
        column1.ColumnGroupName = "SingleAction";
        column1.DataField = "ShipCountry";
        column1.ItemStyle.Width = 50;
         
    PlaceHolder1.Controls.Add(RadGrid1);
}

Thanks,
Princy
0
Saadettin
Top achievements
Rank 1
answered on 30 Oct 2013, 03:43 PM
Thanks. It worked.I want to ask one more question. I created table but now I did not export it to word or excel. How can I achieve this?Thanks.
0
Princy
Top achievements
Rank 2
answered on 31 Oct 2013, 07:05 AM
Hi Saadettin,

If you are Ajaxifying your RadGrid make sure you have disabled it during export. The exporting feature of the control work with regular postbacks only. I have added the following line of code to the above sample code, please try and let me know if any concern. Please provide your full code snippet if this doesn't help.

ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart">
</telerik:RadAjaxManager>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadGrid1, PlaceHolder1, null);
}
 
protected void Page_Init(object sender, EventArgs e)
{
    GetGrid();
}
private void GetGrid()
{
    RadGrid1.ID = "RadGrid1";
  . . . . .
    RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
    RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
    RadGrid1.MasterTableView.CommandItemSettings.ShowExportToWordButton = true;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.ExportSettings.IgnorePaging = true;
 . . . . ..
}

JS:
<script type="text/javascript">
    function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("ExportTo") >= 0)
            args.set_enableAjax(false);
    }
</script>

Thanks,
Princy

0
Saadettin
Top achievements
Rank 1
answered on 31 Oct 2013, 07:47 AM
It worked. But there is one more issue.


Name                 Surname                              Info
                                                        Date                   Number
------------------------------------------------------------------------------
John                  White                     4.4.2011                  45
Tracey              Dapt                       5.4.2011                  70    

For this grid, it was exported to word. But Name and Surname columns doesn't shown in word document. That is, in word document, it is shown like below :

                                                                     Info
                                                        Date                   Number
------------------------------------------------------------------------------
John                  White                     4.4.2011                  45
Tracey              Dapt                       5.4.2011                  70    


Why can it happen?
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2013, 04:39 AM
Hi Saadettin,

I couldn't replicate the issue at my end. The headers are exporting fine at my end. Can you provide your full code snippet so that I may help to resolve the issue.

Thanks,
Princy
Tags
Grid
Asked by
Saadettin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Saadettin
Top achievements
Rank 1
Share this question
or