Binding a DataSet to a ListObject

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.
 
J

\Ji Zhou [MSFT]\

Hello Ed White,

We encounter the "Bad index" COM error because the base index of the
DataTable does not match the base index of the ListObject. The dot Net
collection object always takes 0 as its base index while the office
collection object uses 1. So, we just need to make a little modification,
changing the listTblCols(c) to listTblCols(c+1) in your codes. I have
tested with the following version which works fine.

Dim listTblCols As Excel.ListColumns = listTbl.ListColumns
For c = 0 To twsTable.Columns.Count - 1
listTblCols(c+1).Name = twsTable.Columns(c).ColumnName
Next c

Please have a try and let me know if it works for you. Have a good day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
E

Ed White

Another problem, based upon the same code above: The DataBodyRange of the
ListObject listTbl does not work. It appears to be returning DBNull.
Shouldn't this return the Range of the ListObject?
 
J

\Ji Zhou [MSFT]\

Hello Ed,

Based on my test, the listTbl.DataBodyRange will return the range of data.
The following are two scenarios,

*Before we execute the line "listTbl.DataSource = twsTable", the
listTbl.DataBodyRange will return Nothing in my side. I think this may be
what you have observed.

*After we execute the line "listTbl.DataSource = twsTable", the
listTbl.DataBodyRange will return an Excel range which represents the data
rect. In my side, the listTbl.DataBodyRange.Address is "$A$3:$B$6".

Is this what you see in your side? Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 
E

Ed White

Interesting...when I add listTbl.DataBodyRange to the Watch window, and click
the + sign to the left of it, the only thing that comes up is
m_ObjectToDataMap with a value of Null. However, if I add
listTbl.DataBodyRange.Address, it does return the address. Normally, when
you add something to the Watch window, you can observe all of its properties
by clicking the + sign, but it's not working that way with this, and that is
why I thought is wasn't working.

Incidentally, I get the same results with listTbl.HeaderRowRange.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top