IFeatureRenderer.PrepareFilter returns error on layer with grouping

Discussion created by linkspa on Jul 15, 2010
Latest reply on Jul 28, 2010 by linkspa
Dear all,

In order to find the correct ISymbol of a feature in a FeatureRenderer, I need to prepare the filter (PrepareFilter). This works well for most layers, except for a layer with grouping enabled (with UniqueValueRenderer on 3 fields).

This is my code:

public ISymbol GetFeatureSymbol(IFeature selectedFeature, ILayer selectedLayer)
    IGeoFeatureLayer geoFeatureLayer = selectedLayer as IGeoFeatureLayer;
    IFeatureRenderer featureRenderer = geoFeatureLayer.Renderer as IFeatureRenderer;
    IFeatureLayerDefinition featureLayerDef = selectedLayer as IFeatureLayerDefinition;
    IQueryFilter queryFilter = new QueryFilterClass();
        if (featureLayerDef.DefinitionExpression != "")
            queryFilter.WhereClause = featureLayerDef.DefinitionExpression;
            featureRenderer.PrepareFilter(geoFeatureLayer.FeatureClass, queryFilter);
            queryFilter.WhereClause = geoFeatureLayer.FeatureClass.OIDFieldName + " > 0";
            // this causes an error on some layers
            featureRenderer.PrepareFilter(geoFeatureLayer.FeatureClass, queryFilter);
        // get the symbol
        return featureRenderer.get_SymbolByFeature(selectedFeature);
    catch (Exception ex)
        MessageBox.Show(string.Format("An error occured in 'featureRenderer.PrepareFilter':\n{1}", ex.ToString()));
        return null;

The error is:

System.Runtime.InteropServices.COMException (0x80004005):
Error HRESULT E_FAIL has been returned from a call to a COM component.
at ESRI.ArcGIS.Carto.IFeatureRenderer.PrepareFilter(IFeatureClass fc, IQueryFilter queryFilter)
at GetFeatureSymbol(IFeature selectedFeature, ILayer selectedLayer)

Does somebody now why and what to do?

Many thanks,

PS How can I format the code nicely? All indentation is removed when I post it.... :-(