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

Programmatic RadGrid Creation for Exporting to Excel.

5 Answers 800 Views
Grid
This is a migrated thread and some comments may be shown as answers.
GP
Top achievements
Rank 1
GP asked on 11 Mar 2011, 08:16 PM
I am trying to export a programmatically created radgrid to excel.  I am exporting Data only.  I tried to export as HTML, but that caused Excel to freeze up.  (Passing 1,000 records.)  Part of the export requires that I change the styling on the items (background color), and the other part changes the dataformatstring to {0:C} for currency fields.

Is there a way to accomplish this?  I tried a few suggestions I saw on here, but I'm stumped.  Any help is appreciated.  Thank you.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim radgrid2 As New RadGrid
    radgrid2.AutoGenerateColumns = True
    radgrid2.ID = "RadGrid2"
    Me.PlaceHolder1.Controls.Add(radgrid2)
 
    connSql.ConnectionString = sqlConn.ToString()
 
    Dim poSqlCom As New SqlCommand("dbo.getCurrQtrAll", connSql)
    poSqlCom.CommandType = CommandType.StoredProcedure
    poSqlCom.Parameters.AddWithValue("@UID", "SomeoneElse")
 
 
    Dim adapter As New SqlDataAdapter(poSqlCom)
    Dim links As New DataTable()
 
    Try
        adapter.Fill(links)
    Catch ex As Exception
        'lblResult.Text = ex.Message
        If connSql.State = ConnectionState.Open Then
            connSql.Close()
        End If
    End Try
 
    radgrid2.DataSource = links
    radgrid2.Rebind()
 
    radgrid2.ExportSettings.Excel.FileExtension = "xls"
    radgrid2.ExportSettings.FileName = "Cur_Qtr_Rpt"
    radgrid2.ExportSettings.ExportOnlyData = True
    radgrid2.ExportSettings.IgnorePaging = True
    radgrid2.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML
    radgrid2.MasterTableView.ExportToExcel()
End Sub

5 Answers, 1 is accepted

Sort by
0
GP
Top achievements
Rank 1
answered on 15 Mar 2011, 02:56 PM
Basically, I want to call a radgrid2.excelmlrowcreated.  Is that possible?  Something like radgrid2.OnExcelMLExportRowCreated?  I'm trying to find it in the documentation or through intellisense, but nothing seems obvious.

Please help.
0
Daniel
Telerik team
answered on 16 Mar 2011, 05:39 PM
Hello GP,

There is no way to invoke the event directly. If you call RadGrid1.MasterTableView.ExportToExcel it will be triggered automatically (provided that you have set ExportSettings Excel-Format="ExcelML").

I will do my best to provide an alternative solution if you post some more information about the desired functionality.

Best regards,
Daniel
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
GP
Top achievements
Rank 1
answered on 16 Mar 2011, 06:06 PM
As you see in the initial code, I was calling radgrid2.mastertableview.exporttoexcel(), but it was not firing the rowcreated or styles created automatically.  Also, I was using ExcelML and exportdataonly for format types.  I tried to create
Private Sub RadGrid2_ExcelMLExportRowCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs) Handles RadGrid2.ExcelMLExportRowCreated

and 

Private Sub RadGrid2_ExcelMLExportStylesCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs) Handles RadGrid2.ExcelMLExportStylesCreated

but the handlers threw compiler errors.  I tried just creating the subroutines without the handlers, but these did not fire.

We ended up deciding to put a radgrid2 on the source code, and set the visible property to false.  Then call it's export function from the Button1_Click subroutine.

Though inelegant in solution, it works.  Performance doesn't seem to be hindered, so this is what we will use for now.

Thank you for your help though.
0
Accepted
Daniel
Telerik team
answered on 19 Mar 2011, 10:37 PM
Hello GP,

I recommend that you take advantage of the advanced data-binding. Please download the attached sample project and let me know whether the demonstrated approach is suitable for your scenario.

Best regards,
Daniel
the Telerik team
0
GP
Top achievements
Rank 1
answered on 21 Mar 2011, 04:32 PM
Yes.  This worked.  Thank you very much.
Tags
Grid
Asked by
GP
Top achievements
Rank 1
Answers by
GP
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or