protected void SearchResultsGrid_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Product product = (Product)e.Row.DataItem;
                if (product == null)
                {
                    return;
                }

                DropDownList   crossSellStateList = (DropDownList)e.Row.FindControl("CrossSellState");
                CrossSellState currentState       = GetCrossSellState(_Product.Id, product.Id);
                switch (currentState)
                {
                case CrossSellState.Linked:
                case CrossSellState.CrossLinked:
                    crossSellStateList.SelectedValue = "1";
                    break;

                case CrossSellState.LinksTo:
                    crossSellStateList.SelectedValue = "2";
                    break;

                case CrossSellState.LinkedFrom:
                    crossSellStateList.SelectedValue = "3";
                    break;

                case CrossSellState.Unlinked:
                    crossSellStateList.SelectedValue = "0";
                    break;
                }
            }
        }
        protected void ProductSearchDs_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            CrossSellState csf = (CrossSellState)AlwaysConvert.ToByte(Filter.SelectedValue);

            e.InputParameters["crossSellFilter"] = csf;
            SearchResultsGrid.Columns[2].Visible = ShowImages.Checked;

            if (_DisplayCategorySearch)
            {
                e.InputParameters["categoryId"] = AlwaysConvert.ToInt(HiddenSelectedCategoryId.Value);
            }
        }
        protected CrossSellState GetCrossSellState(int firstProductId, int secondProductId)
        {
            int masterCount = RelatedProductDataSource.GetRelatedCount(firstProductId, secondProductId);
            int childCount  = RelatedProductDataSource.GetRelatedCount(secondProductId, firstProductId);

            CrossSellState crossSellState = CrossSellState.Unlinked;

            if (masterCount > 0 && childCount > 0)
            {
                crossSellState = CrossSellState.CrossLinked;
            }
            else
            if (masterCount > 0 && childCount == 0)
            {
                crossSellState = CrossSellState.LinksTo;
            }
            else
            if (masterCount == 0 && childCount > 0)
            {
                crossSellState = CrossSellState.LinkedFrom;
            }
            return(crossSellState);
        }
        protected void AdjustCrossSellLinking(int firstProductId, int secondProductId, CrossSellState crossSellState)
        {
            RelatedProduct toReleatedProduct   = RelatedProductDataSource.Load(firstProductId, secondProductId);
            RelatedProduct fromReleatedProduct = RelatedProductDataSource.Load(secondProductId, firstProductId);

            switch (crossSellState)
            {
            case CrossSellState.Unlinked:

                if (toReleatedProduct != null)
                {
                    toReleatedProduct.Delete();
                }

                if (fromReleatedProduct != null)
                {
                    fromReleatedProduct.Delete();
                }

                break;

            case CrossSellState.CrossLinked:

                if (toReleatedProduct == null)
                {
                    toReleatedProduct = new RelatedProduct(firstProductId, secondProductId);
                    toReleatedProduct.Save();
                }

                if (fromReleatedProduct == null)
                {
                    fromReleatedProduct = new RelatedProduct(secondProductId, firstProductId);
                    fromReleatedProduct.Save();
                }

                break;

            case CrossSellState.LinksTo:
                if (toReleatedProduct == null)
                {
                    toReleatedProduct = new RelatedProduct(firstProductId, secondProductId);
                    toReleatedProduct.Save();
                }
                if (fromReleatedProduct != null)
                {
                    fromReleatedProduct.Delete();
                }
                break;

            case CrossSellState.LinkedFrom:
                if (toReleatedProduct != null)
                {
                    toReleatedProduct.Delete();
                }
                if (fromReleatedProduct == null)
                {
                    fromReleatedProduct = new RelatedProduct(secondProductId, firstProductId);
                    fromReleatedProduct.Save();
                }
                break;
            }
        }