Populate Combo Box with Query results

1619
9
02-25-2011 07:48 AM
JayKappy
Frequent Contributor
I have a query that is being run....It finds the result and populates a list box. I set teh query withe WHERE clase to act as a wild card search....but now I need a place holder for the multiple returns....
I am tryign this but not having any luck...
Errors in the code:
1. feature2.LayerName
2. .Data = feature2.Attributes

Any thoughts why I cant get the Combo box to populate with the results from the Query?
Its obvious that I have some errors in my code but I can see them or figure out what to change...

This was workign off the Identifty Example....but trying to convert it to run off of search query not a map click...

Thanks in Advance...

VB
    Private _dataItems7 As List(Of DataItem7) = Nothing ' Parcels PID search

    Private Sub ExecutePIDButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim queryTask As New QueryTask("http://gis./services/MG_Test_WGS84/MapServer/8")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTaskFindPID_ExecuteCompletedSearch
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedSpatialQuery
        AddHandler queryTask.Failed, AddressOf QueryTaskFindPID_FailedSearch

        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.Where = String.Format("PID LIKE '%{0}%'", FindPID.Text.Trim())
        query.ReturnGeometry = True
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)
    End Sub

    Private Sub QueryTaskFindPID_ExecuteCompletedSearch(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)

        Dim featureSet As FeatureSet = args.FeatureSet

        ' If an item has been selected            
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerSearch"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then

            _dataItems7 = New List(Of DataItem7)()

                For Each feature As Graphic In featureSet.Features
                    Dim feature2 As Graphic = featureSet.Features
                    Dim title As String = feature2.Attributes.ToString() & " (" & feature2.LayerName & ")"                   
                    _dataItems7.Add(New DataItem7() With {.Title = title, .Data = feature2.Attributes})                    
                    IdentifyComboBox.Items.Add(title)
                Next feature
            End If

            ' Workaround for bug with ComboBox    
            IdentifyComboBox.UpdateLayout()
            IdentifyComboBox.SelectedIndex = 0

' SNIP

End Sub

    Public Class DataItem7
        Private privateTitle As String
        Public Property Title() As String
            Get
                Return privateTitle
            End Get
            Set(ByVal value As String)
                privateTitle = value
            End Set
        End Property
        Private privateData6 As IDictionary(Of String, Object)
        Public Property Data6() As IDictionary(Of String, Object)
            Get
                Return privateData6
            End Get
            Set(ByVal value As IDictionary(Of String, Object))
                privateData6 = value
            End Set
        End Property
    End Class
0 Kudos
9 Replies
JayKappy
Frequent Contributor
Think I got it....
        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then

            IdentifyComboBox5.Items.Clear()

            'If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
            For Each feature As Graphic In featureSet.Features
                Dim Test As String = feature.Attributes("PID")
                IdentifyComboBox5.Items.Add(Test)
            Next feature
            'End If

            ' Workaround for bug with ComboBox    
            IdentifyComboBox5.UpdateLayout()
            IdentifyComboBox5.SelectedIndex = 0
0 Kudos
JayKappy
Frequent Contributor
When I run my query and type in half a PID value I get a bunch of returns....great thats working....but the idenitify example has this going to a _dataItems place holder...
and then setting the listbox index to that on change...

How would I set the appropriate data in the listbox on the combo change?
Especially without the _DataItems place holder seen in my first entry above?

 
   Private Sub cb_SelectionChangedParcels(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim index As Integer = IdentifyComboBox5.SelectedIndex
        If index > -1 Then
             ' SET THE LISTBOX HERE?
        End If

    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
_dataItem is an instance of the DataItem object with Title (IdentifyResult.Value + IdentifyResult.LayerName) and Data (IdentifyResult.Features.Attributes). It acts as a wrapper class to IdentifyResult, where only specific values from the IdentifyResult are retrieved and displayed in the IdentifyGrid.

I think once you know the function of this class and how it plays its part in populating values for the IdentifyGrid (see XAML and code-behind). You can update the sample as you see fit.
0 Kudos
JayKappy
Frequent Contributor
..........yea have been looking at the example....but have questions...so can this work for a query?...am i wasting my time....you are refrerencing using from an Identify which is the example I got this from....but I am not doign an IDENTIFY

I can populate the combobox with this....but then i need to build soem code that will change the data in the grid based off of a combobox change

        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
           
            IdentifyComboBox5.Items.Clear()

            For Each feature As Graphic In featureSet.Features
                Dim Test As String = feature.Attributes("PID")
                IdentifyComboBox5.Items.Add(Test)
            Next feature

            IdentifyComboBox5.UpdateLayout()
            IdentifyComboBox5.SelectedIndex = 0


Is it possible to change the data in the grid similar to this....I assume that I can get the index from the combobox value as seen below...but just cant see how to refresh the grid with that index...
In the idenitify example the index is stored in the _DataItems...
I dont know if this is possible with a Query

thanks

    Private Sub cb_SelectionChangedParcels(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim index As Integer = IdentifyComboBox5.SelectedIndex
        If index > -1 Then
            'IdentifyDetailsDataGrid5. = _dataItems(index).Data            
            IdentifyDetailsDataGrid5.DataContext = index        
End If
0 Kudos
JenniferNery
Esri Regular Contributor
Sure, you can still populate items in combo box with your Query. You will be working with QueryEventArgs.FeatureSet which contains the following members: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Tasks.Feat... instead.

ComboBox is just an ItemsControl. How you define the items to populate it with is completely up to you. Just as in the Identify sample, they used another class to simplify retrieving result and binding to the control. You are free to write your own class or use QueryEventArgs.FeatureSet directly.
0 Kudos
JayKappy
Frequent Contributor
Right after my query is run I am trying this....I get the combobox to populate but I cannot get the records to show in the grid when I change the combobbox value....

Is this approach way of???
Thanks for your help...just trying to see if this angle will work...I think I am halfway there...just cant get the IdentifyDetailsDataGrid5 to populate with the selected ComboBox value

Tryign to set the geomoetry to the results geometry...{.Geometry = result.Geometry

Thanks

    Private Sub QueryTaskFindPID_ExecuteCompletedSearch(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)

        Dim featureSet As FeatureSet = args.FeatureSet

        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
            For Each result As Graphic In featureSet
                Dim identifyParamsParcels As ESRI.ArcGIS.Client.Tasks.IdentifyParameters = New IdentifyParameters() With {.Geometry = result.Geometry, .MapExtent = MyMap.Extent, .SpatialReference = MyMap.SpatialReference, .Width = CInt(Fix(MyMap.ActualWidth)), .Height = CInt(Fix(MyMap.ActualHeight)), .LayerOption = LayerOption.visible}
                ' Set the layer to identify on
                identifyParamsParcels.LayerIds.Add(8)
                Dim identifyTask5 As New IdentifyTask("http://gis/services/MG_Test_WGS84/MapServer")
                AddHandler identifyTask5.ExecuteCompleted, AddressOf IdentifyTask5_ExecuteCompleted
                AddHandler identifyTask5.Failed, AddressOf IdentifyTask5_Failed
                identifyTask5.ExecuteAsync(identifyParamsParcels)
                _dataItems8 = New List(Of DataItem8)()
            Next result

' SNIP SNIP


    Private Sub IdentifyTask8_ExecuteCompleted(ByVal sender As Object, ByVal args As IdentifyEventArgs)
        IdentifyDetailsDataGrid5.DataContext = Nothing

        If args.IdentifyResults IsNot Nothing AndAlso args.IdentifyResults.Count > 0 Then
            If IdentifyResultsPanel5.Visibility = Visibility.Collapsed Then
                IdentifyResultsPanel5.Visibility = Visibility.Visible
                IdentifyGrid5.Height = Double.NaN
                IdentifyGrid5.UpdateLayout()
            End If

            ShowFeatures8(args.IdentifyResults)
        Else
            IdentifyComboBox5.Items.Clear()
            IdentifyComboBox5.UpdateLayout()

            If IdentifyResultsPanel5.Visibility = Visibility.Visible Then
                IdentifyResultsPanel5.Visibility = Visibility.Collapsed
                IdentifyGrid5.Height = Double.NaN
                IdentifyGrid5.UpdateLayout()
            End If

        End If
    End Sub


    Public Sub ShowFeatures8(ByVal results As List(Of IdentifyResult))
        _dataItems8 = New List(Of DataItem8)()

        ' Define the graphics layer to apply the graphic too
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MySelectionGraphicsLayerParcelMapClick"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        If results IsNot Nothing AndAlso results.Count > 0 Then
            IdentifyComboBox5.Items.Clear()
            For Each result As IdentifyResult In results
                Dim feature As Graphic = result.Feature

                ' Add the selected Parcel as graphic
                feature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
                graphicsLayer.Graphics.Add(feature)

                Dim title As String = result.Value.ToString() & " (" & result.LayerName & ")"
                _dataItems8.Add(New DataItem8() With {.Title = title, .Data8 = feature.Attributes})
                IdentifyComboBox5.Items.Add(title)

                feature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), FillSymbol)
                graphicsLayer.Graphics.Insert(0, feature)
            Next result

            ' Workaround for bug with ComboBox 
            IdentifyComboBox5.UpdateLayout()
            IdentifyComboBox5.SelectedIndex = 0

        End If
    End Sub

    Public Class DataItem8
        Private privateTitle As String
        Public Property Title() As String
            Get
                Return privateTitle
            End Get
            Set(ByVal value As String)
                privateTitle = value
            End Set
        End Property
        Private privateData8 As IDictionary(Of String, Object)
        Public Property Data8() As IDictionary(Of String, Object)
            Get
                Return privateData8
            End Get
            Set(ByVal value As IDictionary(Of String, Object))
                privateData8 = value
            End Set
        End Property
    End Class

    Private Sub cb_SelectionChanged8(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim index As Integer = IdentifyComboBox5.SelectedIndex
        If index > -1 Then
            IdentifyDetailsDataGrid5.DataContext = _dataItems8(index).Data8
        End If
    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
It's hard to tell what could be wrong in your application. Try to debug through it and see if ComboBox.Items.Count gets updated. Also, you can change your Binding statements to include a FallbackValue.
For example:
<TextBlock Text="{Binding Path=Value, FallbackValue=BindingToValueFailed}"/>


In this sample, if anything goes wrong with the binding, I will see "BindingToValueFailed" text. I can then check to see what this TextBlock's DataContext is and if it contain a Value property.
0 Kudos
JayKappy
Frequent Contributor
The main problem I think I am facing is that every example I seen is workign from the Identify NOT a query...I can get this to work with a click on the screen...but not from a query


THIS WORKS BUT FROM A MAP CLICK

        
    Private Sub QueryPoint_MouseClick(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
       Dim identifyParamsParcels As ESRI.ArcGIS.Client.Tasks.IdentifyParameters = New IdentifyParameters() With {.Geometry = clickPoint, .MapExtent = MyMap.Extent, .SpatialReference = MyMap.SpatialReference, .Width = CInt(Fix(MyMap.ActualWidth)), .Height = CInt(Fix(MyMap.ActualHeight)), .LayerOption = LayerOption.visible}
        ' Set the layer to identify on
        identifyParamsParcels.LayerIds.Add(8)
        Dim identifyTask5 As New IdentifyTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer")
        AddHandler identifyTask5.ExecuteCompleted, AddressOf IdentifyTask5_ExecuteCompleted
        AddHandler identifyTask5.Failed, AddressOf IdentifyTask5_Failed
        identifyTask5.ExecuteAsync(identifyParamsParcels)
    End Sub


    Private Sub IdentifyTask5_ExecuteCompleted(ByVal sender As Object, ByVal args As IdentifyEventArgs)
        IdentifyDetailsDataGrid5.DataContext = Nothing

        If args.IdentifyResults IsNot Nothing AndAlso args.IdentifyResults.Count > 0 Then
            If IdentifyResultsPanel5.Visibility = Visibility.Collapsed Then
                IdentifyResultsPanel5.Visibility = Visibility.Visible
                IdentifyGrid5.Height = Double.NaN
                IdentifyGrid5.UpdateLayout()
            End If

            ShowFeatures5(args.IdentifyResults)
        Else
            IdentifyComboBox5.Items.Clear()
            IdentifyComboBox5.UpdateLayout()

            If IdentifyResultsPanel5.Visibility = Visibility.Visible Then
                IdentifyResultsPanel5.Visibility = Visibility.Collapsed
                IdentifyGrid5.Height = Double.NaN
                IdentifyGrid5.UpdateLayout()
            End If

        End If
    End Sub

    Public Sub ShowFeatures5(ByVal results As List(Of IdentifyResult))
        _dataItems5 = New List(Of DataItem5)()

        ' Define the graphics layer to apply the graphic too
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MySelectionGraphicsLayerParcelMapClick"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        If results IsNot Nothing AndAlso results.Count > 0 Then
            IdentifyComboBox5.Items.Clear()
            For Each result As IdentifyResult In results
                Dim feature As Graphic = result.Feature

                ' Add the selected Parcel as graphic
                feature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
                graphicsLayer.Graphics.Add(feature)

                Dim title As String = result.Value.ToString() & " (" & result.LayerName & ")"
                _dataItems5.Add(New DataItem5() With {.Title = title, .Data5 = feature.Attributes})
                IdentifyComboBox5.Items.Add(title)

                feature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), FillSymbol)
                graphicsLayer.Graphics.Insert(0, feature)
            Next result

            ' Workaround for bug with ComboBox 
            IdentifyComboBox5.UpdateLayout()
            IdentifyComboBox5.SelectedIndex = 0

        End If
    End Sub



I took some of this existing code and tried to play if off my query: NOTE I am calling this above code with "AddressOf IdentifyTask5_ExecuteCompleted"

1. NOW I get the combobox to return results but I get results circling the actual result...almost like there is a tolerance not set correctly.....As you can see from below I tried to set the tolerance...but for some reason I keep getting results from parcels that are surrounding the actull parcel that shoudl be found... '.Tolerance = 0'....I tried a negative tolerance and got errors...

2. If I have the above code workign on an identify....does it seem logical that I can do the below to get that to return features from a query???

3. One notable change is instead of a map click ".Geometry = clickPoint" I am changing it to ".Geometry = result.Geometry"

4. Additionally it does not find all of the parcels...it seems that when it gets into the tolerance issue and finds additional parcels around the one it should have found it stops....

IS IS LOGICAL to try what I am doing....can I sent the query results below AND the Geomoetry of the results (instead of the map click) to the above code that populates the combobox?
It seems to be sort of working....but extra parcels are beiong found (right next to the one that is supposed to be found....tolerance)

THANKS....hope soemone has some thoughts or suggestions....

    Private Sub QueryTaskFindPID_AddressSearch(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)

        Dim featureSet As FeatureSet = args.FeatureSet

        ' If an item has been selected            
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerSearch"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then

            For Each result As Graphic In featureSet
                Dim identifyParamsParcels As ESRI.ArcGIS.Client.Tasks.IdentifyParameters = New IdentifyParameters() With {.Geometry = result.Geometry, .Tolerance = 0, .MapExtent = MyMap.Extent, .SpatialReference = MyMap.SpatialReference, .Width = CInt(Fix(MyMap.ActualWidth)), .Height = CInt(Fix(MyMap.ActualHeight)), .LayerOption = LayerOption.visible}
                ' Set the layer to identify on
                identifyParamsParcels.LayerIds.Add(8)
                Dim identifyTask5 As New IdentifyTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer")
                AddHandler identifyTask5.ExecuteCompleted, AddressOf IdentifyTask5_ExecuteCompleted                AddHandler identifyTask5.Failed, AddressOf IdentifyTask5_Failed
                identifyTask5.ExecuteAsync(identifyParamsParcels)
            Next result
0 Kudos
JayKappy
Frequent Contributor
I slipped this into my code in my last post righth before I call ShowFeatures and pass the results of the query....as you said to test how many results I am getting....

            MessageBox.Show("Number of results: " & args.IdentifyResults.Count)
            ShowFeatures5(args.IdentifyResults)


I get 4 message boxes with:
Number of results: 5, Number of results: 2, Number of results: 3, Number of results: 4

Now I know there should be 4 returns on this search, which coincides with the 4 mesage boxes....SO i am close.....as I mentioned before this was initially a map click and I converted it to use a query result which are Parcels and have area.....I assume that I am hitting some sort of tolerance and its selecting the parcels that are surrounding the one being found (or in this case the 4 being found)
I think this is whats messing up the return back to the combobox....

Any thoughts?

I think there is soemthing to do with the tolerance...I switched to a point feature and I get 4 returned values...I do it with the Parcels (polygon) feature and I get the original 4 plus their surrounding parcels...

ugggggggg
0 Kudos