private void QuickSerchFetchXml_Callback(IAsyncResult result)
        {
            try
            {
                OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
                EntityCollection     results  = (EntityCollection)response["EntityCollection"];
                foreach (Entity entity in results.Entities)
                {
                    QuickSearchFetchXml qsfx = new QuickSearchFetchXml();

                    qsfx.EntityLogicalName   = entity.GetAttributeValue <string>("gqs_name");
                    qsfx.TransformedFetchXml = entity.GetAttributeValue <string>("gqs_quicksearchformattedfetchxml");
                    qsfx.FetchXml            = entity.GetAttributeValue <string>("gqs_quicksearchfetchxml");

                    //Check for duplicates
                    if (qsfetchXmlList.Where <QuickSearchFetchXml>(a => a.EntityLogicalName.ToUpper() == qsfx.EntityLogicalName.ToUpper()).Count() == 0)
                    {
                        this.qsfetchXmlList.Add(qsfx);
                    }
                }

                this.Dispatcher.BeginInvoke(() => textBoxSearch.IsEnabled = true);
            }
            catch (Exception ex)
            {
                this.ReportError(ex);
            }
        }
        private void prevButton_Click(object sender, RoutedEventArgs e)
        {
            string eln = ((Button)sender).Tag.ToString();

            QuickSearchFetchXml qsfx = this.qsfetchXmlList.Where(a => a.EntityLogicalName == eln).FirstOrDefault <QuickSearchFetchXml>();

            if (qsfx != null)
            {
                ((Button)sender).IsEnabled = false;
                qsfx.PageNumber--;
                GetEntityRecords(qsfx);
            }
        }
        private void ControlNavigationButtons(QuickSearchFetchXml qsfx)
        {
            string stackPanelName = string.Format("StackPanel_{0}", qsfx.EntityLogicalName);
            string gridName       = string.Format("PageGrid_{0}", qsfx.EntityLogicalName);
            string nextButtonName = string.Format("NextButton_{0}", qsfx.EntityLogicalName);
            string prevButtonName = string.Format("PrevButton_{0}", qsfx.EntityLogicalName);

            try
            {
                var stackPanel = ((UIElementCollection)SearchResultGridPanel.Children).Where(a => a.GetType() == typeof(StackPanel) && ((StackPanel)a).Name == stackPanelName).FirstOrDefault <UIElement>();
                if (stackPanel != null)
                {
                    var grid = ((StackPanel)stackPanel).Children.Where(a => a.GetType() == typeof(Grid) && ((Grid)a).Name == gridName).FirstOrDefault <UIElement>();
                    if (grid != null)
                    {
                        var nextButton = ((Grid)grid).Children.Where(a => a.GetType() == typeof(Button) && ((Button)a).Name == nextButtonName).FirstOrDefault <UIElement>();
                        var prevButton = ((Grid)grid).Children.Where(a => a.GetType() == typeof(Button) && ((Button)a).Name == prevButtonName).FirstOrDefault <UIElement>();

                        if (nextButton != null)
                        {
                            if (qsfx.HasMoreRecords)
                            {
                                ((Button)nextButton).IsEnabled = true;
                            }
                            else
                            {
                                ((Button)nextButton).IsEnabled = false;
                            }
                        }

                        if (prevButton != null)
                        {
                            if (qsfx.PageNumber == 1)
                            {
                                ((Button)prevButton).IsEnabled = false;
                            }
                            else
                            {
                                ((Button)prevButton).IsEnabled = true;
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                ReportError(Ex);
            }
        }
        private void GetEntityRecords(QuickSearchFetchXml qsfx)
        {
            string xml = CreateXml(qsfx.FormattedFetchXml, qsfx.PagingCookie, qsfx.PageNumber == 0 ? 1 : qsfx.PageNumber, 5);

            FetchExpression query = new FetchExpression();

            query.Query = xml;

            OrganizationRequest request = new OrganizationRequest()
            {
                RequestName = "RetrieveMultiple"
            };

            request["Query"] = query;
            IOrganizationService service = SilverlightUtility.GetSoapService();

            AsynchMethodParameters param = new AsynchMethodParameters
            {
                Service    = service,
                QSFetchXml = qsfx
            };

            service.BeginExecute(request, new AsyncCallback(GetEntities_Callback), param);
        }
        private void DataBind(DataTable dt, QuickSearchFetchXml qsfx)
        {
            string stackPanelName = string.Format("StackPanel_{0}", qsfx.EntityLogicalName);
            // Create a DataSet and add the table to it
            DataSet ds = new DataSet("EntityDataSet");

            ds.Tables.Add(dt);

            BindableDataGrid.BindableDataGrid myBindableDG = new BindableDataGrid.BindableDataGrid();

            var stackPanel = SearchResultGridPanel.Children.Where(a => a.GetType() == typeof(StackPanel) && ((StackPanel)a).Name == stackPanelName).FirstOrDefault <UIElement>();

            if (stackPanel != null)
            {
                var dataGrid = ((StackPanel)stackPanel).Children.Where(a => a.GetType() == typeof(BindableDataGrid.BindableDataGrid) && ((BindableDataGrid.BindableDataGrid)a).Name == qsfx.DataGridName);
                if (dataGrid != null && dataGrid.Count() == 1)
                {
                    myBindableDG            = (BindableDataGrid.BindableDataGrid)dataGrid.FirstOrDefault <UIElement>();
                    myBindableDG.DataSource = ds;
                    myBindableDG.DataMember = "EntityDataTable";
                    myBindableDG.DataBind();
                }
            }
            else
            {
                //Show DataGrid Only when there are records for given search
                if (dt.Rows.Count > 0)
                {
                    StackPanel entityStackPanel = new StackPanel();
                    entityStackPanel.Name = string.Format("StackPanel_{0}", qsfx.EntityLogicalName);

                    #region Entity DataGrid
                    myBindableDG.Name            = qsfx.DataGridName;
                    myBindableDG.CRMSourceEntity = qsfx.EntityLogicalName;

                    if (ServerBaseUrl.EndsWith("/"))
                    {
                        myBindableDG.ServerBaseUrl = ServerBaseUrl.Substring(0, ServerBaseUrl.Length - 1);
                    }
                    else
                    {
                        myBindableDG.ServerBaseUrl = ServerBaseUrl;
                    }

                    myBindableDG.AutoGenerateColumns = false;
                    myBindableDG.DataSource          = ds;
                    myBindableDG.DataMember          = "EntityDataTable";
                    myBindableDG.DataBind();
                    #endregion

                    #region Label
                    System.Windows.Controls.Label labelEntityName = new System.Windows.Controls.Label();
                    labelEntityName.Content             = XDocument.Parse(qsfx.FormattedFetchXml).Root.Element("entity").Attribute("displayName").Value;
                    labelEntityName.Height              = 20;
                    labelEntityName.MinWidth            = 1000;
                    labelEntityName.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    labelEntityName.FontWeight          = FontWeights.Bold;
                    #endregion

                    #region

                    Image img = new Image();
                    System.Windows.Media.Imaging.BitmapImage imgEntityImg = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://zlcrmweb52/_imgs/ico_16_1.gif"));
                    img.Source             = imgEntityImg;
                    labelEntityName.Height = 16;
                    labelEntityName.Width  = 16;
                    #endregion

                    #region Navigation Grid

                    Grid pagerGrid = new Grid();
                    pagerGrid.Name = string.Format("PageGrid_{0}", qsfx.EntityLogicalName);
                    pagerGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(5, GridUnitType.Star)
                    });
                    pagerGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(5, GridUnitType.Star)
                    });
                    pagerGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;

                    #region Next Button
                    Button nextButton = new Button();
                    nextButton.Content             = "Next";
                    nextButton.Name                = string.Format("NextButton_{0}", qsfx.EntityLogicalName);
                    nextButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    nextButton.Tag    = qsfx.EntityLogicalName;
                    nextButton.Click += new RoutedEventHandler(nextButton_Click);

                    pagerGrid.Children.Add(nextButton);
                    Grid.SetColumn(nextButton, 1);
                    //if (qsfx.HasMoreRecords)
                    //{

                    //    nextButton.IsEnabled = true;
                    //}
                    //else
                    //{
                    //    nextButton.IsEnabled = false;
                    //}
                    #endregion

                    #region Prev Button
                    Button prevButton = new Button();
                    prevButton.Content             = "Prev";
                    prevButton.Name                = string.Format("PrevButton_{0}", qsfx.EntityLogicalName);
                    prevButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    prevButton.Tag    = qsfx.EntityLogicalName;
                    prevButton.Click += new RoutedEventHandler(prevButton_Click);

                    //if (qsfx.PageNumber == 1)
                    //{
                    //    prevButton.IsEnabled = false;
                    //}
                    //else
                    //{
                    //    prevButton.IsEnabled = true;
                    //}
                    pagerGrid.Children.Add(prevButton);
                    Grid.SetColumn(prevButton, 0);
                    #endregion

                    #endregion

                    entityStackPanel.Children.Add(new Line());
                    entityStackPanel.Children.Add(labelEntityName);
                    entityStackPanel.Children.Add(img);
                    entityStackPanel.Children.Add(myBindableDG);
                    entityStackPanel.Children.Add(pagerGrid);

                    SearchResultGridPanel.Children.Add(entityStackPanel);
                }
            }

            ControlNavigationButtons(qsfx);
        }