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

RadGrid Export Issue With GridTemplateColumn

3 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahesh Babu
Top achievements
Rank 1
Mahesh Babu asked on 11 Aug 2009, 12:12 PM
Hi,

I have a radGrid to which I am trying to export using standard radGrid export options. But I am have few issues as below:

1) Export to CSV is not giving me data.
2) Export to Excel is giving data with extra columns.
3) Export to PDF is giving data with extra columns.

I am using the following code to do the same. Please let me know what the issue is.

ExportRadGrid.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportRadGrid.aspx.cs" Inherits="ExportRadGrid" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <telerik:RadScriptManager ID="scrip" runat="server"></telerik:RadScriptManager>
     <telerik:RadGrid ID="radGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true"
                AllowSorting="true" AllowCustomPaging="true" PageSize="5" AllowFilteringByColumn="true"
               Skin="Office2007">
                <PagerStyle Mode="NextPrevNumericAndAdvanced" AlwaysVisible="true" />
               
            </telerik:RadGrid>
            
            <asp:Button ID="btnCSV" Text="CSV" runat="server" OnClick="btnCSV_Click"/>
            <asp:Button ID="btnExcel" Text="Excel" runat="server" OnClick="btnExcel_Click"/>
            <asp:Button ID="btnPDF" Text="PDF" runat="server" OnClick="btnPDF_Click"/>
    </div>
    </form>
</body>
</html>


ExportRadGrid.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class ExportRadGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         GridTemplateColumn column1 = new GridTemplateColumn();
            column1.UniqueName = "RollNo";
            column1.HeaderText = "RollNo";
            column1.ItemTemplate = new LabelTemplate("RollNo", string.Empty);

            GridTemplateColumn column2 = new GridTemplateColumn();
            column2.UniqueName = "Name";
            column2.HeaderText = "Name";
            column2.ItemTemplate = new LabelTemplate("Name", string.Empty);

            GridTemplateColumn column3 = new GridTemplateColumn();
            column3.UniqueName = "Address";
            column3.HeaderText = "Address";
            column3.ItemTemplate = new LabelTemplate("Address", string.Empty);


            radGrid.MasterTableView.Columns.Add(column1);
            radGrid.MasterTableView.Columns.Add(column2);
            radGrid.MasterTableView.Columns.Add(column3);

            radGrid.DataSource = GetDataTable();
            radGrid.DataBind();
        
    }

    protected void btnCSV_Click(object sender, EventArgs e)
    {
        SetReportSettings();
        radGrid.MasterTableView.ExportToCSV();
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        SetReportSettings();
        radGrid.MasterTableView.ExportToExcel();
    }
    protected void btnPDF_Click(object sender, EventArgs e)
    {
        SetReportSettings();
        radGrid.MasterTableView.ExportToPdf();
    }

    private void SetReportSettings()
    {
        this.radGrid.ExportSettings.ExportOnlyData = true;
        this.radGrid.ExportSettings.IgnorePaging = true;
        this.radGrid.ExportSettings.OpenInNewWindow = true;
    }

    private DataTable GetDataTable()
    {
        DataTable dt = new DataTable("MyTable");
        dt.Columns.Add("RollNo");
        dt.Columns.Add("Name");
        dt.Columns.Add("Address");

        DataRow dr1 = dt.NewRow();
        dr1["RollNo"] = 1;
        dr1["Name"] = "Mahesh";
        dr1["Address"] = "Addr1";


        DataRow dr2 = dt.NewRow();
        dr2["RollNo"] = 1;
        dr2["Name"] = "Kishore";
        dr2["Address"] = "Addr2";


        DataRow dr3 = dt.NewRow();
        dr3["RollNo"] = 1;
        dr3["Name"] = "Rajesh";
        dr3["Address"] = "Addr3";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);

        return dt;



    }
}

public class LabelTemplate : ITemplate
{
    public string DataField { get; set; }
    public string DataFormat { get; set; }

    private LabelTemplate()
    {
    }

    public LabelTemplate(string dataField, string dataFormat)
    {
        DataField = dataField;
        DataFormat = dataFormat;
    }

    #region ITemplate Members

    public void InstantiateIn(Control container)
    {
        Label label = new Label();
        label.DataBinding += new EventHandler(Label_DataBinding);
        container.Controls.Add(label);
    }

    void Label_DataBinding(object sender, EventArgs e)
    {
        Label label = (Label)sender;
        GridDataItem container = (GridDataItem)label.NamingContainer;
        if (DataFormat != string.Empty)
            label.Text = string.Format(DataFormat, ((DataRowView)container.DataItem)[DataField].ToString());
        else
            label.Text = ((DataRowView)container.DataItem)[DataField].ToString();
    }

    #endregion
}



Thanks,
Mahesh


3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 12 Aug 2009, 09:57 AM
Hi Mahesh,

Try adding the columns to RadGrid inside the !IsPostBack condition in the PageLoad event. Also since you have statically declared the Grid add columns to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.

CS:
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            GridTemplateColumn column1 = new GridTemplateColumn(); 
 
            // add the column to the columns collection first 
            radGrid.MasterTableView.Columns.Add(column1); 
 
            column1.UniqueName = "RollNo"
            column1.HeaderText = "RollNo"
            column1.ItemTemplate = new LabelTemplate("RollNo"string.Empty); 
 
            GridTemplateColumn column2 = new GridTemplateColumn(); 
            radGrid.MasterTableView.Columns.Add(column2); 
 
            column2.UniqueName = "Name"
            column2.HeaderText = "Name"
            column2.ItemTemplate = new LabelTemplate("Name"string.Empty); 
 
            GridTemplateColumn column3 = new GridTemplateColumn(); 
            radGrid.MasterTableView.Columns.Add(column3); 
 
            column3.UniqueName = "Address"
            column3.HeaderText = "Address"
            column3.ItemTemplate = new LabelTemplate("Address"string.Empty); 
 
 
        
 
        radGrid.DataSource = GetDataTable(); 
        radGrid.DataBind(); 
    } 

You may go through the following help article for getting more details regarding the programmatic creation.
Programmatic creation

Thanks
Princy.
0
kamal
Top achievements
Rank 1
answered on 13 Oct 2011, 06:39 AM
I have a grid with many  GridTemplateColumn. The GridTemplateColumn has a ItemTemplate which is generated dynamically from code-behind.
 here is my code
but when  i export this grid its show me this error

Specified argument was out of the range of valid values.
Parameter name: index

pls tell me how can i do this ?

protected DataTable GetData(int i)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Status", typeof(string));
        table.Columns.Add("Description", typeof(string));
        if (i == 1)
        {
            table.Rows.Add(133, "Available", "USB Mouse");
            table.Rows.Add(542, "Available", "PS/2 Keyboard");
            table.Rows.Add(307, "Not Available", "USB Hub");
            table.Rows.Add(642, "Available", "USB NIC");
        }
        else if (i == 2)
        {
            table.Rows.Add(314, "Not Available", "24\" TFT NEC");
            table.Rows.Add(931, "Not Available", "22\" TFT Iiyama");
            table.Rows.Add(200, "Not Available", "Sapphire Radeon 3870 512MB");
            table.Rows.Add(177, "Available", "AMD Athlon X2 4000+ (tray)");
        }
        else
        {
            table.Rows.Add(001, "test", "24\" TFT NEC");
            table.Rows.Add(002, "arun", "22\" TFT Iiyama");
            table.Rows.Add(003, "vipin", "Sapphire Radeon 3870 512MB");
            table.Rows.Add(004, "harish", "AMD Athlon X2 4000+ (tray)");

        }
        return table;
    }
    private void create_grid()
    {
        RadGrid grd = new RadGrid();
        grd.ID = "grd0";
        form1.Controls.Add(grd);
        grd.ShowHeader = false;
        grd.Skin = "Default";
        grd.AllowFilteringByColumn = false;
        grd.BorderStyle = BorderStyle.None;
        GridTemplateColumn template1 = new GridTemplateColumn();
        template1.UniqueName = "parent";
        template1.ItemTemplate = new parentTemplate();
        grd.MasterTableView.Columns.Add(template1);
        grd.NeedDataSource += new GridNeedDataSourceEventHandler(grd_NeedDataSource);

        Label lbl = new Label();
        lbl.ID = "lbl";
        lbl.Text = "hii kamal how are you";
        form1.Controls.Add(lbl);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid grd = (RadGrid)form1.FindControl("grd0");     
        grd.MasterTableView.ExportToExcel();
      
    }
    void grd_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid grd = (RadGrid)sender;
        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Rows.Add("");
        grd.DataSource = table;
    }

    public class parentTemplate :ITemplate
    {
        protected DataTable GetData(int i)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Status", typeof(string));
            table.Columns.Add("Description", typeof(string));
            if (i == 1)
            {
                table.Rows.Add(133, "Available", "USB Mouse");
                table.Rows.Add(542, "Available", "PS/2 Keyboard");
                table.Rows.Add(307, "Not Available", "USB Hub");
                table.Rows.Add(642, "Available", "USB NIC");
            }
            else if (i == 2)
            {
                table.Rows.Add(314, "Not Available", "24\" TFT NEC");
                table.Rows.Add(931, "Not Available", "22\" TFT Iiyama");
                table.Rows.Add(200, "Not Available", "Sapphire Radeon 3870 512MB");
                table.Rows.Add(177, "Available", "AMD Athlon X2 4000+ (tray)");
            }
            else
            {
                table.Rows.Add(001, "test", "24\" TFT NEC");
                table.Rows.Add(002, "arun", "22\" TFT Iiyama");
                table.Rows.Add(003, "vipin", "Sapphire Radeon 3870 512MB");
                table.Rows.Add(004, "harish", "AMD Athlon X2 4000+ (tray)");

            }
            return table;
        }
        public parentTemplate()
        {

        }      
        public void InstantiateIn(Control control)
        {
            RadGrid grd = new RadGrid();
            grd.ID = "grd1";
            control.Controls.Add(grd);
            grd.NeedDataSource += new GridNeedDataSourceEventHandler(grd_NeedDataSource);

            RadGrid grd1 = new RadGrid();
            grd1.ID = "grd2";
            control.Controls.Add(grd1);
            grd1.NeedDataSource += new GridNeedDataSourceEventHandler(grd1_NeedDataSource);
        }

        void grd1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid grd = (RadGrid)sender;
            grd.DataSource = GetData(2);
        }
       protected void grd_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {

            RadGrid grd = (RadGrid)sender;
            grd.DataSource = GetData(1);
           
        }
    } 

thanks kamal
0
Daniel
Telerik team
answered on 18 Oct 2011, 10:20 AM
Hello Kamal,

Please examine this thread:
Export to excel Dynamic radgrid with GridTemplateColumn

Regards,
Daniel
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
Mahesh Babu
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
kamal
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or