//get suppliers for the selected product from the databse and display in list box
        private void LoadProductSuppliers(int prodId)
        {
            //clear suppliers list
            lvProdSuppliers.Items.Clear();

            //get suppliers from database
            productSupplierList = ProductSupplierDB.GetProductSuppliersByProductID(prodId);

            //populate the controls
            grpProdSupplier.Text = "Suppliers providing product: " + currentProduct.ProdName;


            //filter suppliers list to only Product_Suppliers for the selected product
            var prodSuppliersNames = from productSupplierItem in productSupplierList
                                     join supplierItem in supplierList
                                     on productSupplierItem.SupplierId equals supplierItem.SupID
                                     orderby supplierItem.SupName
                                     select new { productSupplierItem.ProductSupplierId, supplierItem.SupName };

            //populate the list box
            if (prodSuppliersNames != null)
            {
                int i = 0;
                foreach (var item in prodSuppliersNames)
                {
                    lvProdSuppliers.Items.Add(item.ProductSupplierId.ToString());
                    lvProdSuppliers.Items[i].SubItems.Add(item.SupName);
                    i++;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Load data for Product Suppliers tab
        /// </summary>
        /// <param name="productIndex">Current index of Product Supplier list. Optional parameter.</param>
        /// <returns>Rows affected by query, 1 for success and 0 for failure</returns>
        // @author Chi
        private void loadProductSupplierData(int productIndex = 0)
        {
            // Clear list box and data grid view
            lstboxSuppliers.Items.Clear();
            dataGridViewProdSuppliers.Columns.Clear();

            // Set current product's view to index of 'products' list (global variable).
            Products curProduct = products[productIndex];

            curProductSuppliers = ProductSupplierDB.GetProductSuppliersByProductID(curProduct.ProductID);

            // Display current product's ID and Name.
            txtPSProdID.Text   = curProduct.ProductID.ToString();
            txtPSProdName.Text = curProduct.ProductName;

            // Set customized properties for Data Grid View.
            dataGridViewProdSuppliers.AutoGenerateColumns = false;
            dataGridViewProdSuppliers.DataSource          = curProductSuppliers;

            // Created and bind custom column to data source's data property SupplierId.
            DataGridViewTextBoxColumn idColumn = new DataGridViewTextBoxColumn();

            idColumn.Name             = "Supplier's ID";
            idColumn.DataPropertyName = "SupplierId";
            idColumn.Width            = 100;
            idColumn.ReadOnly         = true;

            // Created and bind custom column to data source's data property SupName.
            DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();

            nameColumn.Name             = "Supplier's Name";
            nameColumn.DataPropertyName = "SupName";
            nameColumn.Width            = 239;
            nameColumn.ReadOnly         = true;

            // Add data bound columns to Data Grid View.
            dataGridViewProdSuppliers.Columns.Add(idColumn);
            dataGridViewProdSuppliers.Columns.Add(nameColumn);

            // List of all suppliers
            List <Supplier> allSuppliers = SuppliersDB.GetAllSuppliers();

            // Get a list of suppliers that are currently not added to current product's suppliers list
            filteredSuppliers = new List <Supplier>(allSuppliers.Where(
                                                        Sup => curProductSuppliers.All(
                                                            sup2 => sup2.SupplierId != Sup.SupplierId
                                                            )));

            // Add/Update filtered list to list box to prevent customer from adding
            // same suppliers to product's supplier list.
            foreach (Supplier sup in filteredSuppliers)
            {
                // check if supplier name is null
                if (sup.SupName == null)
                {
                    lstboxSuppliers.Items.Add("");
                }
                else
                {
                    lstboxSuppliers.Items.Add(sup.SupName);
                }
            }
        }