E
Ed White
The code below successfully binds the data in the ListObject listTbl to the
data in the DataTable twsTable. However, it does not bind the column names
or the formatting of twsTable to listTbl. In other words, the column names
on listTbl show up on the Excel spreadsheet as "Column 1", "Column 2", etc.,
and the formatting/column widths is the default for the Excel spreadsheet.
How do I get the column names and formatting from the DataTable bound to the
ListObject?
Dim listTbl As Microsoft.Office.Tools.Excel.ListObject
Dim twsTable As DataTable
'fill twsTable with data and schema from database on SQL Server
fromtwsTable.CommandText = "SELECT * FROM [TWS IntraDay] ORDER BY Ticker"
twsTableDa.Fill(CosDB_ds, "TWS IntraDay")
twsTable = CosDB_ds.Tables("TWS IntraDay")
twsTableDa.FillSchema(twsTable, SchemaType.Source)
'add a ListTable to Excel
listTbl = Globals.Sheet1.Controls.AddListObject(Globals.Sheet1.Range("A2"),
"StockTable")
'bind the list table to the DataTable twsTable
listTbl.DataSource = twsTable
Incidentally, I tried the following:
Dim listTblCols As Excel.ListColumns = listTbl.ListColumns
For c = 0 To twsTable.Columns.Count - 1
listTblCols(c).Name = twsTable.Columns(c).ColumnName
Next c
This returns a "Bad index" COM error when I try to assign or read a .Name
value from listTblCols. When I add listTblCols to the Watch window, it shows
it's m_ObjectToDataMap as null and says "Children could not be evaluated".
However, listTblCols.Count correctly returns 11 for the column count.
data in the DataTable twsTable. However, it does not bind the column names
or the formatting of twsTable to listTbl. In other words, the column names
on listTbl show up on the Excel spreadsheet as "Column 1", "Column 2", etc.,
and the formatting/column widths is the default for the Excel spreadsheet.
How do I get the column names and formatting from the DataTable bound to the
ListObject?
Dim listTbl As Microsoft.Office.Tools.Excel.ListObject
Dim twsTable As DataTable
'fill twsTable with data and schema from database on SQL Server
fromtwsTable.CommandText = "SELECT * FROM [TWS IntraDay] ORDER BY Ticker"
twsTableDa.Fill(CosDB_ds, "TWS IntraDay")
twsTable = CosDB_ds.Tables("TWS IntraDay")
twsTableDa.FillSchema(twsTable, SchemaType.Source)
'add a ListTable to Excel
listTbl = Globals.Sheet1.Controls.AddListObject(Globals.Sheet1.Range("A2"),
"StockTable")
'bind the list table to the DataTable twsTable
listTbl.DataSource = twsTable
Incidentally, I tried the following:
Dim listTblCols As Excel.ListColumns = listTbl.ListColumns
For c = 0 To twsTable.Columns.Count - 1
listTblCols(c).Name = twsTable.Columns(c).ColumnName
Next c
This returns a "Bad index" COM error when I try to assign or read a .Name
value from listTblCols. When I add listTblCols to the Watch window, it shows
it's m_ObjectToDataMap as null and says "Children could not be evaluated".
However, listTblCols.Count correctly returns 11 for the column count.