4 Replies Latest reply on Apr 2, 2013 7:07 AM by izambakci

    Deleting graphics from a feature layer programmatically

    Marvin_84
      I was trying to delete some graphics from my feature layer depending of one attribute, however when I loop on the graphics collection it says that have zero elements.

      
      for (int i = flCalificacion.Graphics.Count - 1; i >= 0; i--)
      {
         grafico = flCalificacion.Graphics[i]; //tomar el elemento gràfico
      
         if (derroterosBorrar.Contains(grafico.Attributes["ASIENTO"].ToString())) //si tiene el mismo nùmero de presentaciòn
         {
                 flCalificacion.Graphics.Remove(grafico); //eliminarlo
         }//fin if
      }//fin for
      
      flCalificacion.SaveEdits();
      



      I'm using the feature layer in Ondemand mode, should I use another one?, or there is another way to get the graphics collection from my feature layer to loop and delete it?.
      Any help will be appreciated
        • Re: Deleting graphics from a feature layer programmatically
          deminhu
          lookt the following  and see if you could find something helpful.
          try
                      {
                          FeatureSet fSet = args.FeatureSet;
                          if (fSet.Features.Count > 0)
                          {
                              foreach (Graphic g in fSet.Features)
                              {
                                  string objId = g.Attributes["OBJECTID"].ToString();

                                  for (int i = 0; i < _plumeFLayer.Graphics.Count; i++)
                                  {
                                      Graphic selectedGraphic = _plumeFLayer.Graphics[i];
                                      if (selectedGraphic.Attributes["OBJECTID"].ToString() == objId)
                                      {
                                          _plumeFLayer.Graphics.Remove(selectedGraphic);
                                          // _plumeFLayer.Update();
                                      }
                                  } // end for
                              } // end foreach
                           // depends how you set AutoSave mode, if it it  true, you don't need the following
                              if (_plumeFLayer.HasEdits)
                              {
                                  _plumeFLayer.SaveEdits();
                                  _plumeFLayer.Update();
                              }
                          }
                         } //e nd try
                      catch (Exception error)
                      {
                       ...
                      }
          • Re: Deleting graphics from a feature layer programmatically
            JNery-esristaff
            It is okay to use OnDemand mode.

            FeatureLayer.Graphics may be empty because UpdateCompleted event is not yet raised. You can subscribe to this and have the following event handler. Note that in OnDemand mode, this will be raised every time your map extent changes so if you want to perform the delete once, you need to unhook from this event.

              private void FeatureLayer_UpdateCompleted(object sender, EventArgs e)
              {
               var l = sender as FeatureLayer;
               if (l.Graphics != null)
               {
                var graphics = l.Graphics.Where(g => (string)g.Attributes["STAGE"] == "tropical depression").ToList();
                foreach (var g in graphics)
                 l.Graphics.Remove(g);
               }
              }
            
            • Re: Deleting graphics from a feature layer programmatically
              mnielsen-esristaff
              Also note that if the layer isn't added to a map (or the map hasn't loaded), the featurelayer will never query for any features (it queries features based on the map's current extent).
              • Re: Deleting graphics from a feature layer programmatically
                izambakci
                You can use update method after initialize FeatureLayer to load graphics . 

                #Sample codes#

                FeatureLayer fl = new FeatureLayer();
                fl.DisableClientCaching = true;
                fl.Mode =  FeatureLayer.QueryMode.OnDemand;
                fl.Url = "http://ServerName/ArcGIS/rest/services/ServiceName/FeatureServer/LayerID";
                fl.OutFields = new OutFields(){"*"};
                fl.Initialize();
                fl.EndSaveEdits += new EventHandler<EndEditEventArgs>((c, d) =>
                {
                    MessageBox.Show("saved");
                });
                fl.Initialized += new EventHandler<EventArgs>((a, b) =>
                {
                    fl.Update();
                    fl.UpdateCompleted += new EventHandler((c, d) =>
                    {
                        fl.Graphics.Remove(fl.Graphics[0]);
                        fl.SaveEdits();
                    });
                });