Deleting graphics from a feature layer programmatically

1229
4
05-04-2012 08:11 AM
MarvinOconitrillo
New Contributor III
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; //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
0 Kudos
4 Replies
DeminHu
New Contributor
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;
                            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)
            {
             ...
            }
0 Kudos
JenniferNery
Esri Regular Contributor
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);
   }
  }
0 Kudos
dotMorten_esri
Esri Notable Contributor
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).
0 Kudos
İsmailZAMBAKCI
New Contributor II
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();
    });
});
0 Kudos