hello..i have OrderForms.cs..it has 2 datagridviews...when i click a product in orderDataGridView then click Add to Cart, it will display the product_id, description, quantity, unitprice, and total on the cartDataGridView..when i change the quantity, the total will change (quantity * unitprice).....then it should show the total bills on the textBox...but how to do it?....please help!...here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InventorySystem
{
    public partial class OrderForm : Form
    {
        public OrderForm()
        {
            InitializeComponent();
            LoadProductsToOrderDataGridView();
            AddDataGridViewButton();
            SetDataGridViewLayout(this.orderDataGridView);
            BindCustomerComboBox();
            SetCartDatagridView();
        }

        private void LoadProductsToOrderDataGridView()
        {
            List<Products> productList = ProductsData.GetProducts();
            if (productList != null && productList.Count > 0)
            {
                this.orderDataGridView.DataSource = productList;
            }
        }

        private void SetDataGridViewLayout(DataGridView datagridview)
        {
            datagridview.Columns[0].Visible = false;
            datagridview.Columns[1].Visible =false;
            datagridview.Columns[2].Width = 75;
            datagridview.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
            datagridview.Columns[4].Width = 50;
            datagridview.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        private void SetCartDatagridView()
        {
            this.cartDataGridView.Columns[0].Visible = false;
            this.cartDataGridView.Rows.Clear();
        }
        private void BindCustomerComboBox()
        {
            List<Customer> customerlist = CustomerData.GetCustomers();
            if (customerlist != null && customerlist.Count > 0)
            {
                customerComboBox.DataSource = customerlist;
                customerComboBox.DisplayMember = "FullName";
                customerComboBox.ValueMember = "CustomerId";
            }
        }

        private void AddDataGridViewButton()
        {
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            button.HeaderText = "Add To Cart";
            button.UseColumnTextForButtonValue = true;
            button.Text = "Add";
            this.orderDataGridView.Columns.Add(button);
        }

        private void orderDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex == 0)
                {
                    int rowcount = this.cartDataGridView.Rows.Count-1;
                   
                    //MessageBox.Show(orderDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString());
                    int pid = (int)orderDataGridView.Rows[e.RowIndex].Cells[1].Value;
                    string desc = orderDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
                    int quantity = (int)orderDataGridView.Rows[e.RowIndex].Cells[5].Value;
                    decimal uprice = (decimal)orderDataGridView.Rows[e.RowIndex].Cells[6].Value;
                    AddToCart addtocart =
                        new AddToCart(pid, desc, quantity, uprice);
                    this.cartDataGridView.Rows.Add();
                    this.cartDataGridView.Rows[rowcount].Cells[0].Value = addtocart.productId;
                    this.cartDataGridView.Rows[rowcount].Cells[1].Value = addtocart.description;
                    this.cartDataGridView.Rows[rowcount].Cells[2].Value = addtocart.quantity;
                    this.cartDataGridView.Rows[rowcount].Cells[3].Value = addtocart.unitPrice;
                    this.cartDataGridView.Rows[rowcount].Cells[4].Value = addtocart.total;
                   
                   
                }
            }
        }

        class AddToCart
        {
            public int productId;
            public string description;
            public int quantity;
            public decimal unitPrice;
            public decimal total;

            public AddToCart(int productid, string description,
                            int quantity, decimal unitprice)
           
                {
                    this.productId = productid;
                    this.description = description;
                    this.quantity = quantity;
                    this.unitPrice = unitprice;
                    this.total = (decimal)(this.quantity * this.unitPrice);
                }
         }
     
        private void button1_Click(object sender, EventArgs e) //Print Receipt Button
        {
            button1.Enabled = false;
            decimal totalbill = 0;
            for(int i = 0;i< cartDataGridView.Rows.Count -1;i++)
            {
                bool succeeded =
                    Orders.InsertOrders((int)this.cartDataGridView.Rows[i].Cells[0].Value,
                    (int)customerComboBox.SelectedValue,
                    Convert.ToInt32(this.cartDataGridView.Rows[i].Cells[2].Value),
                    orderDateTimePicker.Value);
                if(succeeded)
                    totalbill += (decimal)this.cartDataGridView.Rows[i].Cells[4].Value;

            }
            MessageBox.Show("Done");
            textBox1.Text = totalbill.ToString();
            LoadProductsToOrderDataGridView();
            button1.Enabled = true;
        }

    }
}

this code wont add the total bills and it wont show the total sum of bills on the textbox1..pls help

Are you sure you want to use the for loop with Count -1 ?

for(int i = 0;i< cartDataGridView.Rows.Count -1;i++)

I think you want :

for(int i = 0; i < cartDataGridView.Rows.Count ; i++)

If the grid only has the one order, you will not get any total (well total of 0), if you have more than one, it will miss the last one in the grid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.