protected void AcceptedOpportunitiesList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection = e.SortExpression == SortExpression
                                ? (SortDirection == "ASC" ? "DESC" : "ASC")
                                : "ASC";

            SortExpression = e.SortExpression;

            _opportunities.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection);

            AcceptedOpportunitiesList.DataSource = _opportunities;
            AcceptedOpportunitiesList.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfNecessary();

            AssertContactHasParentAccount();

            var opportunities = Enumerable.Empty <Entity>();

            if (string.Equals(CustomerDropDown.Text, "My", StringComparison.InvariantCulture))
            {
                opportunities = ServiceContext.GetOpportunitiesSpecificToContact(Contact)
                                .Where(o => o.GetAttributeValue <OptionSetValue>("statuscode") != null && o.GetAttributeValue <OptionSetValue>("statuscode").Value != (int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Delivered &&
                                       o.GetAttributeValue <OptionSetValue>("statuscode").Value != (int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Declined);
            }
            else             //if (string.Equals(CustomerFilter.Text, "All", StringComparison.InvariantCulture))
            {
                opportunities = ServiceContext.GetOpportunitiesForContact(Contact)
                                .Where(o => o.GetAttributeValue <OptionSetValue>("statuscode") != null && o.GetAttributeValue <OptionSetValue>("statuscode").Value != (int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Delivered &&
                                       o.GetAttributeValue <OptionSetValue>("statuscode").Value != (int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Declined && o.GetAttributeValue <EntityReference>("msa_partnerid") != null &&
                                       o.GetAttributeValue <EntityReference>("msa_partnerid").Equals(Contact.GetAttributeValue <EntityReference>("parentcustomerid")));
            }
            //var searchQuery = Request["query"];

            if (!IsPostBack)
            {
                PopulateCustomerFilter(ServiceContext, Contact);
            }

            HideControlsBasedOnAccess(ServiceContext, Contact);

            if (string.Equals(StatusDropDown.Text, "Open", StringComparison.InvariantCulture))
            {
                opportunities = opportunities.Where(o => o.GetAttributeValue <OptionSetValue>("statecode") != null && o.GetAttributeValue <OptionSetValue>("statecode").Value == (int)Adxstudio.Xrm.Partner.Enums.OpportunityState.Open);
            }
            else if (string.Equals(StatusDropDown.Text, "Won", StringComparison.InvariantCulture))
            {
                opportunities = opportunities.Where(o => o.GetAttributeValue <OptionSetValue>("statecode") != null && o.GetAttributeValue <OptionSetValue>("statecode").Value == (int)Adxstudio.Xrm.Partner.Enums.OpportunityState.Won);
            }
            else if (string.Equals(StatusDropDown.Text, "Lost", StringComparison.InvariantCulture))
            {
                opportunities = opportunities.Where(o => o.GetAttributeValue <OptionSetValue>("statecode") != null && o.GetAttributeValue <OptionSetValue>("statecode").Value == (int)Adxstudio.Xrm.Partner.Enums.OpportunityState.Lost);
            }

            if (!string.IsNullOrEmpty(SearchText.Text))
            {
                opportunities =
                    from o in opportunities
                    join a in ServiceContext.CreateQuery("account") on o.GetAttributeValue <EntityReference>("customerid").Id equals
                    a.GetAttributeValue <Guid>("accountid")
                        where o.GetAttributeValue <EntityReference>("customerid") != null
                        where
                    a.GetAttributeValue <string>("name").ToLower().Contains(SearchText.Text.ToLower()) ||
                    (!string.IsNullOrEmpty(o.GetAttributeValue <string>("adx_referencecode")) &&
                     o.GetAttributeValue <string>("adx_referencecode").IndexOf(SearchText.Text.ToLower(), StringComparison.OrdinalIgnoreCase) >= 0)
                    select o;
            }

            _opportunities = EnumerableExtensions.CopyToDataTable(opportunities.Select(opp => new
            {
                opportunityid = opp.GetAttributeValue <Guid>("opportunityid"),
                ID            = opp.GetAttributeValue <string>("adx_referencecode"),
                Accepted      = opp.GetAttributeValue <DateTime?>("adx_accepteddate").HasValue ? opp.GetAttributeValue <DateTime?>("adx_accepteddate").GetValueOrDefault().ToString("yyyy/MM/dd") : null,
                CompanyName   = opp.GetRelatedEntity(ServiceContext, new Relationship("opportunity_customer_accounts")) != null ? opp.GetRelatedEntity(ServiceContext, new Relationship("opportunity_customer_accounts")).GetAttributeValue <string>("name") : " ",
                City          = opp.GetRelatedEntity(ServiceContext, new Relationship("opportunity_customer_accounts")) != null ? opp.GetRelatedEntity(ServiceContext, new Relationship("opportunity_customer_accounts")).GetAttributeValue <string>("address1_city") : " ",
                Territory     = opp.GetRelatedEntity(ServiceContext, new Relationship("adx_territory_opportunity")) != null ? opp.GetRelatedEntity(ServiceContext, new Relationship("adx_territory_opportunity")).GetAttributeValue <string>("name") : " ",
                Products      = string.Join(", ", opp.GetRelatedEntities(ServiceContext, new Relationship("adx_opportunity_product")).Select(product => product.GetAttributeValue <string>("name"))),
                EstRevenue    = opp.GetAttributeValue <Money>("estimatedvalue") != null ? opp.GetAttributeValue <Money>("estimatedvalue").Value.ToString("C") : null,
                EstClose      = opp.GetAttributeValue <DateTime?>("estimatedclosedate").HasValue ? opp.GetAttributeValue <DateTime?>("estimatedclosedate").GetValueOrDefault().ToString("yyyy/MM/dd") : null,
                AssignedTo    = (opp.GetRelatedEntity(ServiceContext, new Relationship("msa_contact_opportunity")) != null) ? opp.GetRelatedEntity(ServiceContext, new Relationship("msa_contact_opportunity")).GetAttributeValue <string>("fullname") : " ",
                Status        = Enum.GetName(typeof(Adxstudio.Xrm.Partner.Enums.OpportunityState), opp.GetAttributeValue <OptionSetValue>("statecode").Value),
            }).OrderBy(opp => opp.CompanyName));

            _opportunities.Columns["ID"].ColumnName          = "Topic";
            _opportunities.Columns["CompanyName"].ColumnName = "Company Name";
            _opportunities.Columns["EstRevenue"].ColumnName  = "Est. Revenue";
            _opportunities.Columns["EstClose"].ColumnName    = "Est. Purchase";
            _opportunities.Columns["AssignedTo"].ColumnName  = "Assigned To";

            AcceptedOpportunitiesList.DataKeyNames = new[] { "opportunityid" };
            AcceptedOpportunitiesList.DataSource   = _opportunities;
            AcceptedOpportunitiesList.DataBind();
        }