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

[Solved] Export to Excel All columns from database

3 Answers 263 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Manish
Top achievements
Rank 1
Manish asked on 10 Jun 2009, 09:07 AM
Dear Sir,
I have a query during export to excel in a radgrid.
I return 20 columns from the database in the form of a dataset and bind that dataset to radgrid. But in the radgrid, I am not displaying all 20 columns. I am displaying only 5 columns out of that 20 columns. Now, when I export to excel, only records for that 5 columns get exported to excel. I want to export all the 20 columns during export to excel.
I want to display only 5 columns but want to export all the 20 columns returned to me from the dataset from the database.
Please help?

3 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 10 Jun 2009, 10:43 AM
Hello Manish,

You can hide the unwanted columns in the aspx file and then display them back just prior to export:

<Columns> 
    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" Visible="false" /> 
    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" /> 
    <telerik:GridBoundColumn DataField="Other" UniqueName="Other" Visible="false" /> 
</Columns> 


protected void Button1_Click(object sender, EventArgs e) 
    foreach (GridColumn column in RadGrid1.MasterTableView.Columns) 
        column.Visible = true
    RadGrid1.MasterTableView.ExportToExcel(); 

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Jun 2009, 10:50 AM
Hello Manish,

You can set the AutoGenerateColumns property to true and hide the five columns that are currently displayed in the grid inorder to avoid duplicate columns in the Excel sheet, while exporting:
aspx:
<telerik:GridBoundColumn DataField="ContactName" UniqueName="Name" HeaderText="Contact Name"
</telerik:GridBoundColumn> 
<telerik:GridBoundColumn DataField="ContactTitle" UniqueName="Title" HeaderText="Contact Title"
</telerik:GridBoundColumn> 
..... 

c#:
protected void Button1_Click(object sender, EventArgs e) 
    { 
        RadGrid1.MasterTableView.GetColumn("Name").Visible = false
        RadGrid1.MasterTableView.GetColumn("Title").Visible = false
        .... 
        RadGrid1.AutoGenerateColumns = true
        RadGrid1.Rebind(); 
        RadGrid1.ExportSettings.ExportOnlyData = true
        RadGrid1.ExportSettings.IgnorePaging = true
        RadGrid1.ExportSettings.OpenInNewWindow = true
        RadGrid1.MasterTableView.ExportToExcel(); 
        
    } 

Thanks
Princy.
0
Manish
Top achievements
Rank 1
answered on 10 Jun 2009, 12:34 PM
Hey Guys,
Thanks a lot!
Both the answers are valid and correct. I have tried and tested both options.
Thank you very much.
Tags
Grid
Asked by
Manish
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Princy
Top achievements
Rank 2
Manish
Top achievements
Rank 1
Share this question
or