Editing Selected Records

Discussion created by bbulla on May 13, 2010
Latest reply on May 13, 2010 by bbulla

I'm trying to edit multiple features in multiple layers at the same time.  I have some looping setup that loops through all the selected features in one layer, then moves to the next layer, until all the layers have been looked at.  This is working fine.

What's happening though is that after all the features in the first layer get edited, none of the features in any of the following layers get edited.

When I debug, my code that looks to see if anything is selected determines that nothing else is selected after that first layer is finished with.

Why would the rest of my features get 'unselected'??  Here is my code:

//start editing
                UID editorUID = new UIDClass();
                editorUID.Value = "esriEditor.Editor";
                IEditor2 editor = m_application.FindExtensionByCLSID(editorUID) as IEditor2;
                enumLayer.Reset();  //reset before looping through the layers again
                pLayer = (ILayer2)enumLayer.Next();  //set to start at the first layer in the map

while (pLayer != null)  //Starting with the first layer, add the selected features from each layer to a SelectionSet
                    fSel = (IFeatureSelection)pLayer;
                    selSet = (ISelectionSet2)fSel.SelectionSet;

                    if (selSet.Count == 0)  //if no Selected Features in the current layer, then go to next layer.
                        pLayer = (ILayer2)enumLayer.Next();

                    IFeatureLayer2 featureLayer = (IFeatureLayer2)pLayer;
                    IDataset dataset = (IDataset)featureLayer.FeatureClass;


                    //create a cursor
                    ICursor cursor;

                    selSet.Update(null, false, out cursor); 
                    IFeatureCursor fCursor = (IFeatureCursor)cursor;  //create an updatedable FeautreCursor of the
                                                                      //selected features
                    feature = fCursor.NextFeature();  //go to the first feature in the cursor

                    while (feature != null) //loop through all the features in the cursor
                        if (((feature.get_Value(feature.Fields.FindField("FACILITYID"))) == null) || ((feature.get_Value(feature.Fields.FindField("FACILITYID"))).ToString() == ""))
                            //TODO:  use the selected feature and update its GRIDNO field

                            featureClass = gridFLayer.FeatureClass;                           

                            IGeometry queryGeometry = feature.Shape;   //get the geometry of the selected feature                        
                            ISpatialFilter spatialFilter = new SpatialFilterClass();   //create a new spatial filter
                            spatialFilter.Geometry = queryGeometry;
                            spatialFilter.GeometryField = featureClass.ShapeFieldName;
                            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                            spatialFilter.SubFields = "GRIDNO";     //the fieldname to filter for

                            int nameFieldPosition = featureClass.FindField("GRIDNO");   //gets the index of the field

                            IFeatureCursor gridCursor = featureClass.Search(spatialFilter, false);  //create a new cursor
                            IFeature gridFeature = null;    //create a new feature object

                            while ((gridFeature = gridCursor.NextFeature()) != null)    //loop until no more features
                                string name = Convert.ToString(gridFeature.get_Value(nameFieldPosition));   //stores the value in the fieldname
                                //MessageBox.Show("GRIDNO = " + name);
                                feature.set_Value(feature.Fields.FindField("GRIDNO"), name);    //update the selected feature with the value in the fieldname                                                         
                            MessageBox.Show("FACILITYID already exists");

                        fCursor.UpdateFeature(feature);     //do I need this????

                        feature = fCursor.NextFeature();  //go to the next selected feature in the cursor

                    //get the next selected feature in the layer
                    pLayer = (ILayer2)enumLayer.Next();

                //update the Attribute window to show changes
                ISelectionEvents selEvents = (ISelectionEvents)map;