Select to view content in your preferred language

Questions about featuredatagrip and new querytask

1969
16
Jump to solution
07-10-2012 07:26 AM
JaredWhite
Regular Contributor
Hi, I'm trying to make a datagrid that autopopulates with a handful of counties. I believe the new featuredatagrid and the querytask would be the best way to do this so I can keep the parameters dynamic as to which counties are in the datagrid. I have two questions about this, how would I go about only returning specific, pre-set results through the querytask (is it through use of the "Where" public property or "ObjectID")? And my second question is, how would I pre-set the featuredatagrid to autozoom to selected Item instead of autozoom to selected being toggle off by default. I can't seem to find that property setter anywhere in the API reference.
Thanks for any help.
0 Kudos
16 Replies
JaredWhite
Regular Contributor
Url is definitely fine. I had run this code
 Where = string.Format("CNAME LIKE '%{0}%'", FindText.Text), 
instead of the sql string you helped with, accompanied with a FindText box and it returns reuslts. The erro message has given something interesting though.


[ATTACH=CONFIG]16003[/ATTACH]
For some reason I'm getting a security exception. Any clues as to how to fix this?
0 Kudos
JaredWhite
Regular Contributor
Solved
It was a problem with the URL as you had suggested. Code works perfectly now that I specified the port. For some reason my server has decided to only return query results sometimes if the port isn't specified. But that's a fix for another day.

Now, zoomto isn't working properly.

I'm using this for an autozoom
        
        private void CountiesDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            foreach (Graphic g in e.AddedItems)
            {
                g.Select();
                TabletMap.ZoomTo(g.Geometry);
            }
            foreach (Graphic g in e.RemovedItems)
                g.UnSelect();
        }


However, whenever my app runs the query, the screen goes blank white. No crash, no debug throwback, not decernable issues seen in fiddler. Just blank screen. Is my code correct here or does it need to be changed?
0 Kudos
JoeHershman
MVP Alum
If the geometry is a point ZoomTo really does not work so good, you want to expand the point into an envelope or use the ZoomToResolution.  I assume you are only selecting one item at a time, because if multiple items are selected that could mess it up a bit because ZoomTo is called for each Graphic which it might not like
Thanks,
-Joe
0 Kudos
JaredWhite
Regular Contributor
The geometry is polygonal, so I shouldn't have to expand to an envelope, and I am only selecting one at a time. I got the problem to throw an exception. According to my script log, it says

Message: System.InvalidCastException: Unable to cast object of type 'ESRI.ArcGIS.Client.Toolkit.DataSource.TempTypeMinus258081229' to type 'ESRI.ArcGIS.Client.Graphic'.
[ATTACH=CONFIG]16120[/ATTACH]
Any ideas?

Current Code is attached:

 private void RunWestTexasCountyQuery()
        {
            ClearAllGraphics();
            GraphicsLayer searchGraphicsLayer = TabletMap.Layers["SearchGraphicsLayer"] as GraphicsLayer;

            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query()
            {
                ReturnGeometry = true,
                OutSpatialReference = TabletMap.SpatialReference,
                Where = "CNAME IN ('CULBERSON', 'JEFF DAVIS', 'LOVING', 'REEVES', 'WARD', 'WINKLER')"
            };
            
            query.OutFields.Add("CNAME");

            QueryTask queryTask = new QueryTask("<serviceURL>");
            queryTask.ExecuteCompleted += (s, a) =>
            {
                foreach (Graphic g in a.FeatureSet.Features)
                    searchGraphicsLayer.Graphics.Add(g);
            };
            queryTask.ExecuteAsync(query);
            queryTask.Failed += QueryTask_Failed;    
        }

        private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query execute error: " + args.Error);
        }

        private void BookmarksDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            foreach (ESRI.ArcGIS.Client.Graphic g in e.AddedItems)
            {   
                g.Select();
                TabletMap.ZoomTo(g.Geometry);
            }
            foreach (ESRI.ArcGIS.Client.Graphic g in e.RemovedItems)
                g.UnSelect();
        }
0 Kudos
JoeHershman
MVP Alum
[LEFT]This comes from code that Jennifer Nery from ESRI posted once a while back.  The SelectedItem on the data grid is not a Graphic is actually an internal object.  It shows how you can use reflection to get from the AddedItem to the actual graphic it represents.


            object obj = e.AddedItems[0];
            //This bit of trickery is from a post that Jennifer made on the forum to convert the SelectedItem to a graphic using reflection
            MethodInfo methodInfo = obj.GetType().GetMethod("GetGraphicSibling");


            if ( methodInfo == null ) return;


            Graphic g = methodInfo.Invoke(obj, null) as Graphic;



Good luck[/LEFT]
Thanks,
-Joe
0 Kudos
JaredWhite
Regular Contributor
I know i'm probably being overly dense here, but the code is now throwing the exception

"A local variable named 'g' cannot be declared in this scope because it would give a different meaning to 'g', which is already used in a 'parent or current' scope to denote something else"

And I'm not sure how to get around it.

      private void BookmarksDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object obj = e.AddedItems[0];
            //This bit of trickery is from a post that Jennifer made on the forum to convert the SelectedItem to a graphic using reflection
            MethodInfo methodInfo = obj.GetType().GetMethod("GetGraphicSibling");
            if (methodInfo == null) return;
            Graphic g = methodInfo.Invoke(obj, null) as Graphic;
            foreach (Graphic g in e.AddedItems)
            {
                g.Select();
                TabletMap.ZoomTo(g.Geometry);
            }
            foreach (Graphic g in e.RemovedItems)
                g.UnSelect();
        }

0 Kudos
JaredWhite
Regular Contributor
Got it. Thanks for all the help Joe!

        private static Graphic GetGraphicSibling(object item)
        {
            if (item != null)
            {
                MethodInfo mi = item.GetType().GetMethod("GetGraphicSibling");
                if (mi != null)
                    return mi.Invoke(item, null) as Graphic;
            }
            return null;
        }

        private void BookmarksDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in e.AddedItems)
            {
                var g = GetGraphicSibling(item);
                g.Select();
                TabletMap.ZoomTo(g.Geometry);
            }
            foreach (var item in e.RemovedItems)
            {
                var g = GetGraphicSibling(item);
                g.UnSelect();
            }
        }
0 Kudos