Add a column to a VSTO ListObject that has an existing data source?

1

Does anyone know how to add a column to an existing list object in Excel, one that has already got a data source bound to it from a SQL source? I just want to add another column with a formula in every cell for that column of data, to match whatever amount of data is showing at the time.

I have the below 2 examples of what I have tried so far, 1. Gives a syntax error only at runtime of Syntax error: Missing operand before '=' operator. 2. Gives an obscure COM exception.

1.

    Dim prodDateCol = New DataColumn("Prod Date", GetType(System.Int32),
    "=LEFT([Production Code],4)")
    ComplaintsListObject.ListColumns.Add(prodDateCol)

2.

    Dim objListCol = ProdListObject.ListColumns.Add()
    Dim listColRange = objListCol.DataBodyRange
    listColRange.Cells(1, 1) = "=LEFT([Production Code],4)"

I know alternatively I could do this using the Excel cells after first checking each time the list object changes in size, but before looking into how to do that in more detail.. I was hoping there was a way to do this using the List Object itself.

Anyone got any examples or suggestions?

Thanks

UPDATE: I have adjusted this slightly for testing and also looked more at the use of .Expression and it only allows certain functions, so I have changed LEFT to SUBSTRING. This example still returns a COM exception of Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

    Dim prodDateCol As DataColumn = New DataColumn
    prodDateCol.ColumnName = "Prod Date"
    prodDateCol.DataType = System.Type.GetType("System.Int32")
    prodDateCol.Expression = "SUBSTRING([Production Code],1,4)"

    ComplaintsListObject.ListColumns.Add(prodDateCol)
vb.net
winforms
excel
vsto
asked on Stack Overflow Mar 9, 2013 by Pricey • edited Mar 9, 2013 by Pricey

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0