I need to export some data to Excel 97/2003 format. We use Infragistics controls extensively in our applications so I wanted to use the built-in functionality of the Ultragrid to write my Excel file.
I am referencing the Infragistics2.Win.***.v9.2 assemblies in this application.
The class that will be creating the Excel file is a Class Library ( .dll ) project not a WinForms project.
I am creating an instance of the UltraGrid dynamically, then attempting to bind a list of our custom object to it and write the resulting grid to Excel. The Excel file always comes out empty. When I step through the debugger, I look at the datasource of the grid after I set the datasource and it looks fine, but if I look at the Rows property, the count is 0. I've tried forcing an Update() and a Refresh() on the grid after setting the datasource and that doesn't help. I'll paste in sample code to show you exactly what I'm doing. I've switched away from our custom object to a test object for simplicity and it still exhibits the same behavior.
This is the method I'm using to create the Excel file. The "AddressExportObject" is a test class that just has 3 string properties.
So, the final question: Why are there no rows in my grid after I set the datasource?
Thanks,
-BEP
private string SaveExcelDoc() { UltraGrid grdAddresses = new UltraGrid(); UltraGridExcelExporter oExcelExporter = new UltraGridExcelExporter(); string sFilename = @"C:\TestOutFile.xls"; BindingList lstAddresses = new BindingList(); lstAddresses.Add(new AddressExportObject("Cust1", "101 Main St", "Suite 2")); lstAddresses.Add(new AddressExportObject("Cust2", "300 Park Way", "")); grdAddresses.DataSource = lstAddresses; grdAddresses.DataBind(); oExcelExporter.Export(grdAddresses, sFilename, Infragistics.Excel.WorkbookFormat.Excel97To2003); return (sFilename); }
Hi BEP,
This question is answered here:
How to export to excel from an UltraWinGrid - Infragistics Community
Thank you, Mike. I missed that one in my searches.
I have my application working exactly how I want it, now.
The exporter only exports the data in the grid. So if your grid only knows about one page of data, then that's all that will be exported.
You could, of course, create a new grid and bind it to your whole data source. You would have to copy any property settings from your real grid into the new grid, though. This probably isn't too hard. You could use grid.DisplayLayout.CopyFrom to copy the layout from your real grid into the export grid.
Then you just have to deal with any row- or cell-level properties. Maybe hook up the export grid to the same InitializeLayout and InitializeRow event handlers as your real grid so that it applies the same settings.
Mike,
If i am using UltraGrid, I have the paging implemented on my own ( i don’t use inbuilt paging available for UltraGrid if any), I want to export all the page data when i export to excel.
Currently it just exports single page data which is shown to user.
Is there any way to export all the data to excel when loaded from database?