Question How can I subtract sum from DataGridView?

Godis

Member
Joined
Jul 31, 2019
Messages
19
Programming Experience
Beginner
I am working on a sales project and I finally want my btnStockLevel_Click (object sender, EventArgs e)
{

}
to subtract the gross sales from the gross purchases, but I am totally stuck.

I don't know the code to put in there in order to work satisfactorily. Please I will appreciate if someone would help me out.

I hereby attach a section of my project code.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


 namespace GyesiBizzApp
{
    public partial class StockAndSales : Form
    {
        decimal UnitCost, TotalCost, UnitSale, TotalSales;
        int PurchaseQty, SalesQty, StockLevel, GrossPurchaseQty, GrossSalesQty;
       

    public StockAndSales()
        {
            InitializeComponent();
        }

        private void salesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.salesBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.bizzDBDataSet);
        }

        private void StockAndSales_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bizzDBDataSet.Sales' table. You can move, or remove it, as needed.
            this.salesTableAdapter.Fill(this.bizzDBDataSet.Sales);          
        }    

        private void purchaseQtyTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            int.TryParse(purchaseQtyTextBox.Text, out PurchaseQty);
            purchaseQtyTextBox.Text = PurchaseQty.ToString();
            purchaseQtyTextBox.Text = string.Format("{0:0}", PurchaseQty);

            TotalCost = PurchaseQty * UnitCost;
            totalCostTextBox.Text = TotalCost.ToString();
            totalCostTextBox.Text = string.Format("{0:#,###.00}", TotalCost);

            StockLevel = GrossPurchaseQty - GrossSalesQty;
            stockLevelTextBox.Text = StockLevel.ToString();
            stockLevelTextBox.Text = string.Format("{0:0}", StockLevel);
        }

        private void unitCostTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            decimal.TryParse(unitCostTextBox.Text, out UnitCost);
            unitCostTextBox.Text = UnitCost.ToString();
            unitCostTextBox.Text = string.Format("{0:#,###.00}", UnitCost);

            TotalCost = PurchaseQty * UnitCost;
            totalCostTextBox.Text = TotalCost.ToString();
            totalCostTextBox.Text = string.Format("{0:#,###.00}", TotalCost);

            StockLevel = GrossPurchaseQty - GrossSalesQty;
            stockLevelTextBox.Text = StockLevel.ToString();
            stockLevelTextBox.Text = string.Format("{0:0}", StockLevel);
        }

        private void salesQtyTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            int.TryParse(salesQtyTextBox.Text, out SalesQty);
            salesQtyTextBox.Text = SalesQty.ToString();
            salesQtyTextBox.Text = string.Format("{0:0}", SalesQty);

            TotalSales = SalesQty * UnitSale;
            totalSalesTextBox.Text = TotalSales.ToString();
            totalSalesTextBox.Text = string.Format("{0:#,###.00}", TotalSales);

            StockLevel = GrossPurchaseQty - GrossSalesQty;
            stockLevelTextBox.Text = StockLevel.ToString();
            stockLevelTextBox.Text = string.Format("{0:0}", StockLevel);
        }

        private void untiSaleTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            decimal.TryParse(unitSaleTextBox.Text, out UnitSale);
            unitSaleTextBox.Text = UnitSale.ToString();
            unitSaleTextBox.Text = string.Format("{0:####.00}", UnitSale);

            TotalSales = SalesQty * UnitSale;
            totalSalesTextBox.Text = TotalSales.ToString();
            totalSalesTextBox.Text = string.Format("{0:#,###.00}", TotalSales);

            StockLevel = GrossPurchaseQty - GrossSalesQty;
            stockLevelTextBox.Text = StockLevel.ToString();
            stockLevelTextBox.Text = string.Format("{0:0}", StockLevel);
        }

        private void btnGrossPurchase_Click(object sender, EventArgs e)
        {
            grossPurchaseQtyTextBox.Text = (from DataGridViewRow row in salesDataGridView.Rows
                               where row.Cells[7].FormattedValue.ToString() != string.Empty
                               select Convert.ToInt32(row.Cells[7].FormattedValue)).Sum().ToString();
        }

        private void btnGrossSales_Click(object sender, EventArgs e)
        {
           
            grossSalesQtyTextBox.Text = (from DataGridViewRow row in salesDataGridView.Rows
                                         where row.Cells[17].FormattedValue.ToString() != string.Empty
                               select Convert.ToInt32(row.Cells[17].FormattedValue)).Sum().ToString();          
        }

        private void btnStockLevel_Click(object sender, EventArgs e)
        {
                     
        }
    }
}
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,245
Location
Chesapeake, VA
Programming Experience
10+
Lines 98-111 seem to show that you are already computing the gross sums, but putting the values directly into your textboxes. Why not also put the values into your class variables for the gross sums, and which you seem to be using in lines 38-96 to compute and show your stock levels?
 

Latest posts

Top Bottom