How to remove standalone table from contents

166
2
01-28-2020 04:20 PM
KhamilleSarmiento
New Contributor II

Hello! I am trying to remove a specified standalone table from the Contents pane. Here is the code I have so far:

try
{
     var mapView = MapView.Active;
     if (mapView != null)
     {
          var map = mapView.Map;
          if (map != null)
          {
               foreach (StandaloneTable table in map.StandaloneTables)
               {
                    if (table.Name.Contains("K Factors"))
                       map.RemoveStandaloneTable(table);
               }
          }
     }
}
catch (Exception ex)
{
}

There may be multiple tables whose name contains "K Factors" and that's ok -- I would like them all removed. 

I noticed that map.StandaloneTables is a ReadOnlyObservableCollection and I'm not sure if this has to do with the exceptions that I'm getting.

The exception says either, {"Class not registered\r\n\r\nClass not registered\r\n"} or {"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}

Any help would be appreciated. 

Reply
0 Kudos
2 Replies
RichRuh
Esri Regular Contributor

The RemoveStandaloneTable() method must be called on the MCT.  Try this:

QueuedTask.Run(()=>
{
map.RemoveStandaloneTable(table);
}

See Working with Multithreading in ArcGIS Pro for more information.

KhamilleSarmiento
New Contributor II

This was very helpful, thank you. I was able to get it to work. 

Reply
0 Kudos