Select to view content in your preferred language

How to set the Where clause for the FeatureLayer binded to a Feature Datagrid?

1364
7
02-07-2011 11:24 AM
weiliang
Deactivated User
Hi,

I have a FeatureLayer which is binded to a FeatureDatagrid, and I want to set the Where clause for this FeatureLayer in the code. How could I do this? The following is what I do now, but it seems doesn't work. Do I miss something?

FeatureLayer GPgl2 = new FeatureLayer();
GPgl2 = this.mainPage.MyMap.Layers["WellPermitsFL"] as FeatureLayer;
GPgl2.Where = String.Format("{0}='{1}'", "Type", "Expired");
GPgl2.Refresh();

Thanks for your input.

Wei
0 Kudos
7 Replies
JenniferNery
Esri Regular Contributor
Here's your code with minor changes. You don't need to create a new instance of FeatureLayer and if you are changing parameters to your FeatureLayer query, you need to call Update() http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureLay.... Refresh will redraw the current set of graphics.

//FeatureLayer GPgl2 = new FeatureLayer();
FeatureLayer GPgl2 = this.mainPage.MyMap.Layers["WellPermitsFL"] as FeatureLayer; 
GPgl2.Where = String.Format("{0}='{1}'", "Type", "Expired"); 
GPgl2  .Update();
0 Kudos
weiliang
Deactivated User
Many thanks, Jenifer. You resolved my problem. And I have another question about FeatureLayer. Can I dynamically add a FeatureLayer in the code rather than predefine in the .xaml?

I tried to use the following code to fulfill this but failed (Error message: Object reference not set to an instance of an object.)

My c# code is listed below, could someone check it for me? Thanks,

  FeatureLayer tempWellFL = new FeatureLayer();           
  tempWellFL.Url = "gis.asdf.com/ArcGIS/rest/services/aaa/MapServer/0";
  this.mainPage.MyMap.Layers.Add(tempWellFL);
  tempWellFL.Update();

Have a great day!

Wei
0 Kudos
JenniferNery
Esri Regular Contributor
Anything you can do in XAML, you can do in code-behind.

This is close however I am curious with this line:

FeatureLayer tempWellFL = new FeatureLayer();  
tempWellFL.Url = "gis.asdf.com/ArcGIS/rest/services/aaa/MapServer/0"; 
this.  mainPage.MyMap.Layers.Add(tempWellFL); 
//tempWellFL.Update(); 


If MyMap is the name of your map, you can do MyMap.Layers.Add(tempWellFL). Maybe mainPage is null?

Also, you don't want to call Update() when there are no changes to the query and the layer has not been initialized.
0 Kudos
weiliang
Deactivated User
Thanks for you quick reply, Jennifer. I do want to use the update to reflect the Where clause (I deleted it to avoid the confusion). It is the featurelayer's update function shoot me error message. Before this line of code been executed, I can see that the featurelayer is been successfully added into the MyMap control. Any thoughts that why the update doesn't work?

BTW, if I use .xaml predefined FeatureLayer, there is no such problem.

FeatureLayer tempWellFL = new FeatureLayer();
tempWellFL.Url = "gis.asdf.com/ArcGIS/rest/services/aaa/MapServer/0";
this.mainPage.MyMap.Layers.Add(tempWellFL);
tempWellFL.Where = "1=1";
tempWellFL.Update();

Thanks,

Wei
0 Kudos
JenniferNery
Esri Regular Contributor
The error is calling Update() before the layer is initialized. Since you are just adding the layer to your Map.Layers, the layer is not initialized yet.

Also, you don't need to call Update() if the Where clause is not changing. In your code, you are setting Where clause for the first time and don't need to call Update(). If you will  be changing Where clause later on and need to re-query the service, then you can call Update().
0 Kudos
weiliang
Deactivated User
Thanks, Jennifer. So, how can I initialize this feature layer in the code?
0 Kudos
JenniferNery
Esri Regular Contributor
Once the layer is added to the map, it gets initialized, you don't need to explicitly call Initialize(). You can subscribe to its Initialized event, if you need to do something right after the layer is initialized. In your code snippet though, you don't need to call Update() since it is the first time you are querying the service. If you will be updating Where clause again, check whether layer.IsInitialized before you call Update().
0 Kudos