Query Features
QueryFeaturesButton.cs
// Copyright 2011 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using ESRI.ArcGISExplorer;
using ESRI.ArcGISExplorer.Application;
using ESRI.ArcGISExplorer.Mapping;
using ESRI.ArcGISExplorer.Geometry;
using ESRI.ArcGISExplorer.Data;
using ESRI.ArcGISExplorer.Threading;

namespace QueryFeaturesCS
{
    /// <summary>
    /// Implements a custom query features button
    /// </summary>
    /// <remarks>
    /// Allows the user to digitize a polygon on the map, then uses
    /// the resulting geometry to query the selected feature layer
    /// and display the results in a DataGridView on a form.
    /// The grid is populated by binding the query results using
    /// a TableBindingAdapter.
    /// </remarks>
    public class QueryFeaturesButton : ESRI.ArcGISExplorer.Application.Button
    {
        /// <summary>
        /// Raised when the button is clicked on the ribbon
        /// </summary>
        public override void OnClick()
        {
            // get the first selected item in the map. It's safe to assume the item is a
            // feature layer because the declaration of this button in the AddIns.xml specifies
            // the E3_FeatureLayerSelectedCondition condition
            FeatureLayer layer = ESRI.ArcGISExplorer.Application.Application.SelectedItems[0] as FeatureLayer;

            // allow the user to digitize a polygon on the map
            Polygon polygon = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.TrackPolygon();
            try
            {
                // query the feature layer for the features that intersect the polygon
                RowCollection rowCollection = layer.Table.Search(new Filter(polygon, FilterSearchOptions.Intersects));

                // create the table binding adapter and fill it
                TableBindingAdapter tableBindingAdapter = new TableBindingAdapter(rowCollection);
                tableBindingAdapter.UseColumnAliasNames = true;
                tableBindingAdapter.UseCodedValueDomains = true;
                tableBindingAdapter.Fill();

                // display the results
                QueryResultsForm resultsForm = new QueryResultsForm(tableBindingAdapter);
                resultsForm.ShowDialog();

            }
            catch (NullReferenceException)
            {

                System.Windows.Forms.MessageBox.Show("No features were selected. Please try again.");
            }
        }
    }
}