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

RadGrid is not exporting the data for custom ItemTemplate column

3 Answers 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tigger Tag
Top achievements
Rank 1
Tigger Tag asked on 28 Apr 2014, 10:10 PM
Hi,

I have a RadGrid with empty columns list inside a RadAjaxPanel. I add all columns on page load event. I have one column which needs to show multiple hyperlinks, so I create a class that implements ITemplate and set the column's Itemtemplate to this new class as show below.


public class HyperlinksTemplateColumn : ITemplate {
 
       
      protected Label _value;
 
      public void InstantiateIn(System.Web.UI.Control container) {
         _value = new Label();
          
         _value.ID = "_hyperlinkLabel" ;        
          
         container.Controls.Add(_value);
      }
       
   }

GridTemplateColumn customCol = new GridTemplateColumn();
radGrid.MasterTableView.Columns.Add(customCol);
customCol.ItemTemplate = new HyperlinksTemplateColumn();

All other columns are GridBoundColumns. When I export the grid, all columns except this custom column exports the data fine. I tried setting data in ItemCommand on excel export command as show below.

protected void radGrid_ItemCommand(object sender, GridCommandEventArgs e) {
   if (e.CommandName == RadGrid.ExportToExcelCommandName) {
      foreach (GridDataItem item in radGrid.MasterTableView.Items) {
          item[CmSwAgreementConstants.HYPERLINK_COLUMN_UNIQUE_NAME].Text = "set corresponding data her"
      }
   }
}


Can anyone please help why the export for the custom column is not working. 

Thank you in advance,
Ana
 

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 29 Apr 2014, 10:59 AM
Hi,

Can you try setting the ExportOnlyData property to false and check if it helps. Its hard to identify the issue with so less information so please provide you full code snippet.

C#:
radGrid.ExportSettings.ExportOnlyData = false;

Thanks,
Princy
0
Tigger Tag
Top achievements
Rank 1
answered on 29 Apr 2014, 12:14 PM
Thank you Princy for the response. It looks like I need to create the RadGrid instance in the code behind in Page Init, unlike the way I have now with Radgrid skeleton defined in ascx file, since I have a custom template column class. Is my understanding correct?

I tried setting ExportOnlyData to false and it still didn't work.
0
Princy
Top achievements
Rank 1
answered on 30 Apr 2014, 05:00 AM
Hi,

Please provide your full code snippet, without it its hard to identify the issue. Meanwhile try the below sample code snippet, and find the difference with your code.

ASPX:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />

ASCX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

ASCX.CS:
RadGrid RadGrid1;
protected void Page_Init(object source, System.EventArgs e)
{
    RadGrid1 = new RadGrid();
    RadGrid1.MasterTableView.DataKeyNames = new string[] { "OrderID" };
    RadGrid1.PageSize = 15;
    RadGrid1.AllowPaging = true;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
    RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
    RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.ExportSettings.IgnorePaging = true;
    RadGrid1.ExportSettings.OpenInNewWindow = true;
    RadGrid1.ExportSettings.HideStructureColumns = true;
 
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "OrderID";
    boundColumn.HeaderText = "OrderID";
    boundColumn.SortExpression = "OrderID";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "ShipCountry";
    boundColumn.HeaderText = "ShipCountry";
    boundColumn.SortExpression = "ShipCountry";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
 
    GridTemplateColumn customCol = new GridTemplateColumn();
    customCol.ItemTemplate = new HyperlinksTemplateColumn();
    customCol.HeaderText = "ShipName";
    RadGrid1.MasterTableView.Columns.Add(customCol);
 
    this.PlaceHolder1.Controls.Add(RadGrid1);
}
void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetDataTable("SELECT  * FROM Orders");
}
 
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    return myDataTable;
}
 
public class HyperlinksTemplateColumn : ITemplate
{
    protected Label lblValue;
    public void InstantiateIn(System.Web.UI.Control container)
    {
        lblValue = new Label();
        lblValue.ID = "lblShipName";
        container.Controls.Add(lblValue);
        lblValue.DataBinding += new EventHandler(lblValue_DataBinding);
    }
 
    void lblValue_DataBinding(object sender, EventArgs e)
    {
        Label l = (Label)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        l.Text = ((DataRowView)container.DataItem)["ShipName"].ToString() + "<br />";
    }
}

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