Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Binding a DataSet to a ListObject


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.Shee t1.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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Binding a DataSet to a ListObject

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 , 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:
.

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Binding a DataSet to a ListObject

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 , 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:
.

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Binding a DataSet to a ListObject

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 , 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:
.

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Binding a DataSet to a ListObject

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 , 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:
.

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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Binding a DataSet to a ListObject

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 , 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:
.

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListObject binding samsam Excel Programming 0 September 19th 06 08:05 AM
assign dataset to matching dataset Michael Dirksen Excel Programming 0 June 16th 06 04:34 PM
Converting A Quarterly Dataset to Weekly Dataset Dan Thompson Excel Programming 5 November 25th 05 09:27 PM
Late Binding examples of binding excel application HeatherO Excel Programming 13 March 17th 05 08:19 AM
EARLY binding or LATE binding ? jason Excel Programming 6 February 26th 04 04:57 PM


All times are GMT +1. The time now is 11:55 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"