+ Reply to Thread
Results 1 to 5 of 5

Thread: Deleting graphics from a feature layer programmatically

  1. #1
    Marvin Oconitrillo
    Join Date
    Jan 2012
    Posts
    16
    Points
    0
    Answers Provided
    2


    0

    Post Deleting graphics from a feature layer programmatically

    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.

    Code:
    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

  2. #2
    Demin Hu
    Join Date
    Apr 2010
    Posts
    136
    Points
    2
    Answers Provided
    0


    0

    Default Re: Deleting graphics from a feature layer programmatically

    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)
    {
    ...
    }

  3. #3
    Jennifer Nery

    Join Date
    May 2010
    Posts
    2,449
    Points
    289
    Answers Provided
    51


    0

    Default Re: Deleting graphics from a feature layer programmatically

    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.

    Code:
    		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);
    			}
    		}
    Jennifer Nery
    Software Developer
    ArcGIS API for SL/WPF/WP7

  4. #4
    Morten Nielsen

    Join Date
    Oct 2009
    Posts
    1,166
    Points
    44
    Answers Provided
    7


    0

    Default Re: Deleting graphics from a feature layer programmatically

    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).
    /Morten Nielsen
    ArcGIS Runtime SDK for .NET
    silverlight team blog - twitter - personal blog

  5. #5
    ismail zambakci
    Join Date
    Apr 2010
    Posts
    16
    Points
    0
    Answers Provided
    0


    0

    Default Re: Deleting graphics from a feature layer programmatically

    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();
    });
    });

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts