AnsweredAssumed Answered

Fixing and updating selection layers with ArcObjects

Question asked by domrorke on Aug 20, 2014

I've two questions relating to updating layers in the ArcMap TOC.


Question 1)  I want to update a layer in the ArcMap TOC with a new selectionLayer of the same type.  I have the code to generate the selectionLayer (an IFeatureLayer), but as you will see below, I am currently adding this into the TOC as a new layer.


I’m creating a selection layer from a Feature Layer using IFeatureLayerDefinition2.CreateSelectionLayer(). 


The input FeatureLayer is a IGDBRasterCatalogLayer:


ESRI.ArcGIS.Geodatabase.IRasterCatalog rasterCatalog = GeodatabaseUtilities.OpenFGDBRasterCatalog(Properties.Settings.Default.pathToRasterCatalog, Properties.Settings.Default.rasterCatalogName);

IFeatureClass rasterCatFeatureClass = rasterCatalog as IFeatureClass;


ESRI.ArcGIS.Carto.IGdbRasterCatalogLayer pGdbRastercatalogLayer = new GdbRasterCatalogLayerClass();



IFeatureSelection featSelection = pGdbRastercatalogLayer as IFeatureSelection;

featSelection.SelectionSet = pSelectionset;


IFeatureLayerDefinition2 pFeatLayerDef2 = pGdbRastercatalogLayer as IFeatureLayerDefinition2;

IFeatureLayer selectionLayer = pFeatLayerDef2.CreateSelectionLayer("Test", true, "", "");

selectionLayer.Name = layerName;


This code works, and I can add the resulting Feature Layer (selectionLayer in above code) to the ArcMap TOC using:

ESRI.ArcGIS.Carto.IMap map = GetMapFromArcMap(ArcMap.Application);



Whilst this works, it would be far better to be able to assign the IFeatureLayer object to the existing ILayer in the TOC, but I can’t see how.  It would be great if someone can indicate how to achieve this.



Question 2)  A separate but related question:  If I fix a broken data source using the ArcMap UI on a selection layer, the selectionset persists.  When I try this with the following code, the layers data source is fixed, but all features from the new data source are returned, instead of just the selection set. 


IFeatureLayer featureLayer = (IFeatureLayer)layerInTOC;

// Change FeatureClass of layer

       featureLayer.FeatureClass = newFeatureClass;


So it would be great to hear how to fix the selection layer data source and keep the selectionSet?


Environment: ArcGIS Basic 10.2 desktop, Windows 7, C#, ArcObjects


Many thanks,