private void DGResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //Catch Any singlerecords displayed as picklists
            if (_MatchCache.Count > 0)
            {
                if (_MatchCache.Peek().TypeOfResult == MatchResultCodes.SingleMatch)
                {
                    this.FoundRecord = _MatchCache.Peek();
                    BuildColumnLookup();
                    this.DialogResult = DialogResult.OK;
                    return;
                }
            }

            RunMatch(_LocatorId, _MatchType, _Query, _UseFuzzy, true, DGResults.CurrentRow.Index);
            DGResults.DataSource = _CurrentDataTable;

            DGResults.Refresh();

            //enable the back button as now we are 1 step down the results tree
            butBack.Enabled = true;
        }
        /// <summary>
        /// Runs the match.
        /// </summary>
        /// <param name="inLocatorId">The in locator id.</param>
        /// <param name="inMatchType">Type of the in match.</param>
        /// <param name="inQuery">The in query.</param>
        /// <param name="inFuzzy">if set to <c>true</c> [in fuzzy].</param>
        /// <param name="indrillDown">if set to <c>true</c> [indrill down].</param>
        /// <param name="inSelectedItem">The in selected item.</param>
        private void RunMatch(string inLocatorId, String inMatchType, String inQuery, bool inFuzzy, bool indrillDown, int inSelectedItem)
        {
            try
            {

                //set RoecordID & CacheID to be empty
                String RecordID = "";
                String CacheID = "";

                //if its a drill down then grab the RecordId and CacheID
                if (indrillDown)
                {
                    RecordID = _MatchCache.Peek().PickListItems[inSelectedItem].RecordId.ToString();
                    CacheID = _MatchCache.Peek().CacheIdentifier;
                }

                //Run First Query and Pop results onto stack
                MatchResult matchResult = this.Client.Match(inLocatorId, inMatchType, inQuery, inFuzzy, RecordID, _SpatialReference, CacheID);

                switch (matchResult.TypeOfResult)
                {
                    case MatchResultCodes.PickList:

                        //Produce a DataTable for the Datagrid to show
                        _CurrentDataTable = ConvertToDataTable(matchResult);

                        //cache query for drilldown
                        _MatchCache.Push(matchResult);
                        break;

                    case MatchResultCodes.SingleMatch:

                        //code for case where show if parent haschildren but Singlerecord is returned
                        if (_MatchCache.Count != 0)
                        {
                            if (_MatchCache.Peek().PickListItems[inSelectedItem].HasChildren)
                            {
                                //show in datgrid as a single record
                                DataTable dataTable = new DataTable();
                                dataTable.Columns.Add("Drill", typeof(Bitmap));
                                dataTable.Columns.Add("Score");
                                dataTable.Columns.Add("Description");

                                DataRow dataRow = dataTable.NewRow();
                                Bitmap bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("DataHubServicesAddin.Images.Search.bmp"));
                                dataRow[0] = bitmap;
                                dataRow[1] = matchResult.MatchedRecordScore;
                                dataRow[2] = matchResult.MatchedRecord.R.V[2/*"LOCATOR_DESCRIPTION"*/].Replace("|LOCATOR_SEPARATOR|", ",");
                                dataTable.Rows.Add(dataRow);
                                _CurrentDataTable = dataTable;
                                _MatchCache.Push(matchResult);
                            }
                            else
                            {
                                //set the found item
                                this.FoundRecord = matchResult;
                                BuildColumnLookup();

                                //inform all that the dialog has been closed with OK
                                DialogResult = DialogResult.OK;
                            }

                        }
                        else
                        {
                            //set the found item
                            this.FoundRecord = matchResult;
                            BuildColumnLookup();
                            //inform all that the dialog has been closed with OK
                            DialogResult = DialogResult.OK;
                        }
                        break;

                    default:
                        this.FailReason = matchResult.TypeOfResult;
                        this.DialogResult = DialogResult.Abort;
                        break;

                }
            }
            catch (Exception ex)
            {
                DataHubExtension.ShowError(ex);
            }
        }
        private DataTable ConvertToDataTable(MatchResult matchResult)
        {
            //Create a new Datatable
            DataTable dataTable = new DataTable();

            try
            {
                //Add Columns
                dataTable.Columns.Add("Drill", typeof(Bitmap));
                dataTable.Columns.Add("Score");
                dataTable.Columns.Add("Description");

                //add a new row for each item in the picklist
                foreach (PickItem pickItem in matchResult.PickListItems)
                {
                    //Create a new DataRow
                    DataRow dataRow = dataTable.NewRow();

                    //Add Data To Row
                    if (pickItem.HasChildren)
                    {

                        Bitmap bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("DataHubServicesAddin.Images.Drill.bmp"));
                        dataRow[0] = bitmap;
                    }
                    else
                    {
                        Bitmap bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("DataHubServicesAddin.Images.Search.bmp"));
                        dataRow[0] = bitmap;
                    }
                    dataRow[1] = pickItem.Score;
                    dataRow[2] = pickItem.Description;

                    //Add Row to Datatable
                    dataTable.Rows.Add(dataRow);

                }
            }
            catch (Exception ex)
            {
                DataHubExtension.ShowError(ex);
            }

            //return the Datatable - all filled up and ready to bind
            return dataTable;
        }