Q:

Export DataTable to Excel using programmatically created DevExpress GridView

I'm trying to export a DataTable to Excel using a programmatically created DevExpress GridControl. The code I have exports the GridView columns, but without any populated data rows. Here is the sample code (tblErrors is a DataTable parameter passed to the method):
GridControl control = new GridControl();                
GridView view = new GridView();
control.ViewCollection.Add(view);
control.MainView = view;
foreach (DataColumn tCol in tblErrors.Columns)
{
    GridColumn gCol = new GridColumn();
    gCol.Name = "col" + tCol.ColumnName;
    gCol.FieldName = tCol.ColumnName;
    gCol.UnboundType = DevExpress.Data.UnboundColumnType.Bound;
    view.Columns.Add(gCol);
    gCol.Visible = true;
}
control.DataSource = tblErrors;
control.Visible = true;
view.ExportToXlsx(String.Format(@"{0}\{1} Error Report.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), DateTime.Today.ToString("yyyy-MM-dd")));

Any ideas as to what I'm missing?

A:

At the time you're calling view.ExportToXlsx this view (and the whole GridControl) is not finally initialized and loaded the data.
You can check it very simply - just set breakpoint on view.ExportToXlsx call and you can see that in this moment view.RowCount == 0, so nothing to export yet.
In fact, this "final initialization" will occur when control became visible. But in your code you're not adding  control to the Controls collection of Form or UserControl, so in fact it will never be visible, and has no rows.
If it is intended behaviour and you want just to export data from your dataTable to Excel using GridControl as some "adaptor" and without showing this GridControl on form - you can make some trick like
control.DataSource = tblErrors;
Controls.Add(control);
control.ForceInitialize();
view.ExportToXlsx(....
Control.Remove(control);

Note it may cause some "blinking" of control if your data amount in tblErrors is large because control is actually being added to form, then force initialized, exported data and then control being removed from form. 
As for me - it is not the best approach to export data from dataTable to Excel. Consider using instead some libraries for direct writing to Excel files, fro example, NPOI.
UPDATE
After some thoughts - you can prevent "blinking" I've mentioned just by setting control.Visible = false; before adding control to form. But this approach using GridControl is still not the best from my point of view.

