Select to view content in your preferred language

Cant remove graphic from FeatureLayer in code.

1898
2
06-14-2011 07:48 AM
CraigGallant
Deactivated User
Hello,

I have a button that creates a query task and finds a graphic. Now this works fine and on the QueryTask complete I asign the feature to a graphic. Then i try to remove this graphic from the FeatureLayer but it does not remove it. I can break the code and see the new graphic is the right one I want to delete from the feature layer but it does not delete it. Is there something im doing wrong?

Thanks for the help,
Craig




private void btnUploadDelete_Click(object sender, RoutedEventArgs e)
{

       queryTask = new QueryTask(_clientInfo.PlanPointFeatureLayer);
       queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
       Query query = new Query();
       query.Where = "UniqueID = '" + uniqueID + "'";
       query.ReturnGeometry = true;
       query.OutSpatialReference = new SpatialReference(102100);
       query.OutFields.Add("*");
       queryTask.ExecuteAsync(query);
}


private void QueryTask_ExecuteCompleted(object sender, QueryEventArgs args)
{
       FeatureSet featureSet = args.FeatureSet;
          
       Graphic graphic = new Graphic();

       if (featureSet != null && featureSet.Features.Count > 0)
       {
              foreach (Graphic feature in featureSet.Features)
              {
                     graphic = feature;
               }
       }

       FeatureLayer fLayer = MyMap.Layers["FeatureLayerPoint"] as FeatureLayer;
       fLayer.Visible = true;
       fLayer.Graphics.Remove(graphic);
       fLayer.SaveEdits();
       fLayer.Visible = false;
}
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
When you remove from a collection, you have to give it the same instance. The graphic you get from a QueryTask is not the same instance as the graphics in the FeatureLayer.
Instead you need to find the graphic that matches, and remove that.
For instance using Linq:
var graphic = (from g in fLayer.Graphics where 
g.Attributes["ObjectID"]==uniqueID select g).FirstOrDefault();
if(grahpic != null)
   fLayer.Graphics.Remove(graphic);

From what I can tell in your sample, I don't even think you need to do a QueryTask, if you already know the ObjectID. There's no reason to download a graphic feature that you already have on the client.
0 Kudos
CraigGallant
Deactivated User
Thanks you.

That sure worked better than my way and you were correct I didnt not need to make a call to the server since i already new the ObjectID.


Thansk,
Criag
0 Kudos