Excel ListObject doesn't update on adding new item in data source.

Suresh3594

New Member
Joined
Jul 5, 2017
Messages
7
[FONT=&quot]Hi,[/FONT]
[FONT=&quot]I am working on vsto addin which uses ListObject to create table. This ListObject is binded to a List<> collection and the data shows up fine. The problem is, when user adds a new row in the table by using Right Click->Insert->Table Row Above/Below, It does not add a new entry in datasource. To fix this, I am handling worksheet change event and then check if new row is inserted or not. If its inserted, I will Undo the change and I am adding a new entry in the List<> datasource. [/FONT]
[FONT=&quot]This is how I am doing it:
[/FONT]

Code:
[COLOR=#000000]vstoListObject[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Disconnect[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]Globals[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]ThisAddIn[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Application[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Undo[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]var[/COLOR][COLOR=#000000] newEntry [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]MyClassObject[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataSource[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Insert[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]index[/COLOR][COLOR=#666600],[/COLOR][COLOR=#000000] newEntry[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
vstoListObject[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]SetDataBinding[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]DataSource[/COLOR][COLOR=#666600]);[/COLOR]

[FONT=&quot]For some reason, SetDataBinding is failing and it throws error. This is the error[/FONT]
[FONT=&quot]System.Runtime.InteropServices.COMException was unhandled by user code
HResult=-2146827864
Message=Exception from HRESULT: 0x800A01A8
Source=mscorlib
ErrorCode=-2146827864
StackTrace:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.ListObject.get_Range()
at Microsoft.Office.Tools.Excel.ListObjectImpl.get_Range()
at Microsoft.Office.Tools.Excel.ResizeHandler.InitializeState(Int32 dataRowCount, Int32 dataColumnCount, Boolean& needsNoRowsFix, Boolean& needsOneRowFix)
at Microsoft.Office.Tools.Excel.ResizeHandler.Resize(Int32 dataRowCount, Int32 dataColumnCount, Boolean insertOrRemoveRanges)
at Microsoft.Office.Tools.Excel.ResizeHandler.Resize(ListObjectImpl listObject, Worksheet worksheet, Int32 dataRowCount, Int32 dataColumnCount, Boolean insertOrRemoveRanges)
at Microsoft.Office.Tools.Excel.ListObjectImpl.Resize(Int32 rows, Int32 columns)
at Microsoft.Office.Tools.Excel.ListObjectImpl.SetItemsCore(Boolean checkTypeChanged, Boolean force)
at Microsoft.Office.Tools.Excel.ListObjectImpl.SetDataConnection(Object newDataSource, String newDataMember, String[] newMappedColumns, Boolean force)
at Microsoft.Office.Tools.Excel.ListObjectImpl.SetDataConnection(Object newDataSource, String newDataMember, Boolean force)
at Microsoft.Office.Tools.Excel.ListObjectImpl.SetDataBinding(Object dataSource)
at MyProject.Addin.OnSelectionChange(Range target)[/FONT]
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,822
Messages
6,121,767
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top