Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. calculate totalprice base on checkbox

calculate totalprice base on checkbox

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #1

    Hi, i got this code to calculate the price when i tick the checkbox in my datagridview

            double price = 0.0;
            foreach (DataGridViewRow dgw in dataGridProduct.Rows)
            {
                DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell;
    
                try
                {
                    if ((bool)datacell.Value == true)
                    {
                        //MessageBox.Show("" + dgw.ToString());
    
                        price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString());
                        lblTotalPrice.Text = "Total Price : $ " + price;
                    }
                }
                catch (Exception exc)
                {
                }
            }
    

    When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?

    K L P 3 Replies Last reply
    0
    • B benjamin yap

      Hi, i got this code to calculate the price when i tick the checkbox in my datagridview

              double price = 0.0;
              foreach (DataGridViewRow dgw in dataGridProduct.Rows)
              {
                  DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell;
      
                  try
                  {
                      if ((bool)datacell.Value == true)
                      {
                          //MessageBox.Show("" + dgw.ToString());
      
                          price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString());
                          lblTotalPrice.Text = "Total Price : $ " + price;
                      }
                  }
                  catch (Exception exc)
                  {
                  }
              }
      

      When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?

      K Offline
      K Offline
      KaptinKrunch
      wrote on last edited by
      #2

      Check the state of the checkbox and use the appropriate operator.

      if(checkbox1.checked)
      price += Convert.ToDouble(dgw.Cells[6].Value.ToString());
      else
      price -= Convert.ToDouble(dgw.Cells[6].Value.ToString());

      Just because we can; does not mean we should.

      1 Reply Last reply
      0
      • B benjamin yap

        Hi, i got this code to calculate the price when i tick the checkbox in my datagridview

                double price = 0.0;
                foreach (DataGridViewRow dgw in dataGridProduct.Rows)
                {
                    DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell;
        
                    try
                    {
                        if ((bool)datacell.Value == true)
                        {
                            //MessageBox.Show("" + dgw.ToString());
        
                            price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString());
                            lblTotalPrice.Text = "Total Price : $ " + price;
                        }
                    }
                    catch (Exception exc)
                    {
                    }
                }
        

        When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What problem are you having? One problem I can see is that you don't have any logic listed to do the price subtraction.

        Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns

        1 Reply Last reply
        0
        • B benjamin yap

          Hi, i got this code to calculate the price when i tick the checkbox in my datagridview

                  double price = 0.0;
                  foreach (DataGridViewRow dgw in dataGridProduct.Rows)
                  {
                      DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell;
          
                      try
                      {
                          if ((bool)datacell.Value == true)
                          {
                              //MessageBox.Show("" + dgw.ToString());
          
                              price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString());
                              lblTotalPrice.Text = "Total Price : $ " + price;
                          }
                      }
                      catch (Exception exc)
                      {
                      }
                  }
          

          When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          You are missing code for when the checkbox is false. Look at the else clause that you have been shown...

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups