Problem with IDisplayRelationshipClass

10-12-2012 01:35 AM
New Contributor
I'm using IDisplayRelationshipClass to create join and delete join.. well, I try, because it's not working at all. Creating join causes deleting each others existing joins and creating new one. Here is my code:

IDisplayRelationshipClass displayRelationshipClass = _layer as IDisplayRelationshipClass;
                IMemoryRelationshipClassFactory memoryRelationshipClassFactory =
                    new MemoryRelationshipClassFactoryClass();

                IFeatureLayer featureLayer = pOriginLayer as IFeatureLayer;
                IObjectClass originObjectClass = featureLayer.FeatureClass;

                IFeatureLayer layerTojoin = pLayerToJoin as IFeatureLayer;
                IObjectClass objectClassToJoin = layerTojoin.FeatureClass;

                IRelationshipClass relationshipClass = memoryRelationshipClassFactory.Open(

                    pIsKeepAllRecordsChecked ? esriJoinType.esriLeftOuterJoin : esriJoinType.esriLeftInnerJoin);

Why it's not working? I'm doing exactly the same things as in thousands samples..
0 Kudos
3 Replies
Regular Contributor III
When you create a join in ArcMap, the join results in a table that is a combination of the two tables you've joined together.  This table is used as the display source for the layer.  If you want to add another join, then you need to join the table to the result of the first join.  This will result in a table that is the combination of all 3 tables.  You can do this by modifying your code to use the layer's display feature class instead of its feature class (IGeoFeatureLayer.DisplayFeatureClass).  If a layer has joins, then the display feature class will be the result of those joins.  Using it when joining a new table will keep all of the previous joins intact.  Removing a join is much more complicated if the layer has multiple tables joined to it.  You have to think of it as a chain of tables:  layer feature class -> first joined table -> second joined table -> third joined table, etc.  To remove a join you have to recreate all of the joins while leaving out the table you no longer want.  You can do this by accessing the layer's RelQueryTable collection.  This collection contains info about the tables that are joined to the layer.  I don't have an example handy so you'll have to search for one.  Basically, you would loop through the RelQueryTable collection and use each RelQueryTable to get the info you need to rejoin all of the tables.  When you get to the table you want to remove, just skip it and go on to the next table in the collection.
0 Kudos
New Contributor
Thanks for answer. However, I have another problem - with implementing your theory.

I have code like this:  (GetLastJoinTable method looks like: )

I can create one join, every next cause COM Exception when executing DisplayRelationshipClass.. What am I doing wrong?
0 Kudos
New Contributor
I get the concept of what you are saying, Neil, but I'm having trouble the implementation.

Using the DisplayFeatureClass how to I join that to the FeatureLayer since it's a feature class and not a FeatureLayer?  

my code is :

rcStationing = FeatureClassHelper.GetRelationshipClassByLabel("STATIONING", fc, esriRelDirection.esriRelDirectionForward);
                rcAttribute = FeatureClassHelper.GetRelationshipClassByLabel("ATTRIBUTES", fc, esriRelDirection.esriRelDirectionForward);

                if (rcAttribute != null && rcAttribute != null)
                    (flayer as IDisplayRelationshipClass).DisplayRelationshipClass(rcStationing, esriJoinType.esriLeftInnerJoin);

                    IFeatureClass joinedFC = (flayer as IGeoFeatureLayer).DisplayFeatureClass;

                    ((flayer as IGeoFeatureLayer).di as IDisplayRelationshipClass).DisplayRelationshipClass(rcAttribute, esriJoinType.esriLeftInnerJoin);

So i'm having a problem figuring out how to display that second join.

Thanks for any help.
0 Kudos