PC Review


Reply
Thread Tools Rate Thread

Binding a DataSet to a ListObject

 
 
Ed White
Guest
Posts: n/a
 
      9th Dec 2008

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.

--
Ed
 
Reply With Quote
 
 
 
 
\Ji Zhou [MSFT]\
Guest
Posts: n/a
 
      10th Dec 2008
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 (v-(E-Mail Removed), 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 Removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Ed White
Guest
Posts: n/a
 
      10th Dec 2008
Such a simple solution! Yes, it works. Thanks
--
Ed


"""Ji Zhou [MSFT]""" wrote:

> 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 (v-(E-Mail Removed), 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 Removed).
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Ed White
Guest
Posts: n/a
 
      10th Dec 2008
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?
--
Ed


"""Ji Zhou [MSFT]""" wrote:

> 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 (v-(E-Mail Removed), 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 Removed).
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
\Ji Zhou [MSFT]\
Guest
Posts: n/a
 
      11th Dec 2008
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 (v-(E-Mail Removed), 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 Removed).

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

 
Reply With Quote
 
Ed White
Guest
Posts: n/a
 
      11th Dec 2008
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.
--
Ed


"""Ji Zhou [MSFT]""" wrote:

> 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 (v-(E-Mail Removed), 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 Removed).
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListObject binding =?Utf-8?B?c2Ftc2Ft?= Microsoft Excel Programming 0 19th Sep 2006 08:05 AM
Binding to DataSet sck10 Microsoft ASP .NET 2 4th Nov 2004 01:36 AM
DataSet Binding Ghost Microsoft Dot NET Compact Framework 6 5th Aug 2004 07:06 AM
Re: Dataset Binding Brian Parlier Microsoft ADO .NET 1 16th Mar 2004 08:21 PM
BUG in Dataset Binding?? Juergen Bronn Microsoft Dot NET Framework Forms 0 17th Jul 2003 11:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:52 AM.