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. Add a listview cell

Add a listview cell

Scheduled Pinned Locked Moved C#
question
10 Posts 2 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.
  • A Offline
    A Offline
    ago2486
    wrote on last edited by
    #1

    I want to add after adding, I had used the datagriedview which works well but for other reasons of redu I decided to use a listview but with this code it adds me that after the second addition, it is to say when I make a first addition, my textbox which receives the sum remains 0 and at the second addition it takes the value of the first and at the third it adds the first and the second, it does not add all the additions, and I do not that's not why. I need an explanation to review what is wrong in the code

    for (int i = 0; i < listView1.Items.Count; i++)
    {
    total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);

                            }
    

    Lbl_Affich_TotalCmd.Text = total_achat.ToString();

    OriginalGriffO 1 Reply Last reply
    0
    • A ago2486

      I want to add after adding, I had used the datagriedview which works well but for other reasons of redu I decided to use a listview but with this code it adds me that after the second addition, it is to say when I make a first addition, my textbox which receives the sum remains 0 and at the second addition it takes the value of the first and at the third it adds the first and the second, it does not add all the additions, and I do not that's not why. I need an explanation to review what is wrong in the code

      for (int i = 0; i < listView1.Items.Count; i++)
      {
      total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);

                              }
      

      Lbl_Affich_TotalCmd.Text = total_achat.ToString();

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      We can't tell: we don't have access to your code or data while it is running. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        We can't tell: we don't have access to your code or data while it is running. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        A Offline
        A Offline
        ago2486
        wrote on last edited by
        #3

        ok thank you i'm getting started

        OriginalGriffO 1 Reply Last reply
        0
        • A ago2486

          ok thank you i'm getting started

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You're welcome!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          A 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            You're welcome!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            A Offline
            A Offline
            ago2486
            wrote on last edited by
            #5

            thank you sir I followed your advice and I saw that the problem came from the fact that at the first addition my variables total_achat and i always remain 0. Frankly I do not know why?.

            private void BtnAjouter_Click(object sender, EventArgs e)
            {
            double total;
            decimal total_achat;

                    //si les champs sont vides
                    if (Lbl\_Affich\_Designation.Text == "" || Lbl\_Affich\_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl\_affich\_TxtQteStock.Text == "" || CmbRef\_Produit.Text == "")
                    {
                        MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //Vérifier si la valeur saisie est numeric
                        if (CmbRef\_Produit.Text != "")
                        {
                            //Vérifier si la commande est inferieur ou egale au stock
                            double a = 0;
                            double b = 0;
                            double c;
                            int nbprod;
                            //Convertire les textBox en int
                            double.TryParse(TxtQteCmd.Text.Trim(), out a);
                            double.TryParse(Lbl\_affich\_TxtQteStock.Text.Trim(), out b);
                            double.TryParse(Lbl\_Affich\_PrixUnitaire.Text.Trim(), out c);
            
                            if (a <= b)
                            {
                                total = a \* c;
                                total\_achat = 0;
                                nbprod = 0;
                                //ADDITIONNER UNE COLONNE DU DATAGRIDVIEW
                               
            
                                try
                                {
                    
                                    
            
                                    using (OleDbCommand cmd = d.sql\_con.CreateCommand())
                                    {
                                        cmd.CommandText = "INSERT INTO Detail\_temp (ref\_det, qute\_det, Designation, Prix\_unitaire\_HT, Prix\_total\_HT) VALUES (@ref\_det,@qute\_det,@Designation,@Prix\_unitaire\_HT,@Prix\_total\_HT)";
                                        cmd.Parameters.AddWithValue("@ref\_det", CmbRef\_Produit.Text);
                                        cmd.Parameters.AddWithValue("@qute\_det", TxtQteCmd.Text);
                                        cmd.Parameters.AddWithValue("@Designation", Lbl\_Affich\_Designation.Text);
                                        cmd.Parameters.AddWithValue("@Prix\_unitaire\_HT", Lbl\_Affich\_PrixUnitaire.Text);
            
            OriginalGriffO 1 Reply Last reply
            0
            • A ago2486

              thank you sir I followed your advice and I saw that the problem came from the fact that at the first addition my variables total_achat and i always remain 0. Frankly I do not know why?.

              private void BtnAjouter_Click(object sender, EventArgs e)
              {
              double total;
              decimal total_achat;

                      //si les champs sont vides
                      if (Lbl\_Affich\_Designation.Text == "" || Lbl\_Affich\_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl\_affich\_TxtQteStock.Text == "" || CmbRef\_Produit.Text == "")
                      {
                          MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                      }
                      else
                      {
                          //Vérifier si la valeur saisie est numeric
                          if (CmbRef\_Produit.Text != "")
                          {
                              //Vérifier si la commande est inferieur ou egale au stock
                              double a = 0;
                              double b = 0;
                              double c;
                              int nbprod;
                              //Convertire les textBox en int
                              double.TryParse(TxtQteCmd.Text.Trim(), out a);
                              double.TryParse(Lbl\_affich\_TxtQteStock.Text.Trim(), out b);
                              double.TryParse(Lbl\_Affich\_PrixUnitaire.Text.Trim(), out c);
              
                              if (a <= b)
                              {
                                  total = a \* c;
                                  total\_achat = 0;
                                  nbprod = 0;
                                  //ADDITIONNER UNE COLONNE DU DATAGRIDVIEW
                                 
              
                                  try
                                  {
                      
                                      
              
                                      using (OleDbCommand cmd = d.sql\_con.CreateCommand())
                                      {
                                          cmd.CommandText = "INSERT INTO Detail\_temp (ref\_det, qute\_det, Designation, Prix\_unitaire\_HT, Prix\_total\_HT) VALUES (@ref\_det,@qute\_det,@Designation,@Prix\_unitaire\_HT,@Prix\_total\_HT)";
                                          cmd.Parameters.AddWithValue("@ref\_det", CmbRef\_Produit.Text);
                                          cmd.Parameters.AddWithValue("@qute\_det", TxtQteCmd.Text);
                                          cmd.Parameters.AddWithValue("@Designation", Lbl\_Affich\_Designation.Text);
                                          cmd.Parameters.AddWithValue("@Prix\_unitaire\_HT", Lbl\_Affich\_PrixUnitaire.Text);
              
              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              So use the debugger to find out! Start by breakpointing the line itself:

              total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);

              When it hits for the first time, the total and the loop index should both be zero because that is what your code told them to be:

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

              After it, you will have added the cell value, so step over that line. What's the value of the total now? If it's still zero, then it means that this code evaluates to zero:

              Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value)

              So look at why that is: what's the value of dataGridView1.Rows[i].Cells[4].Value - use the debugger to find out by firstly hovering over word "Value" and if that looks "right" then adding the calculation to teh Watch pane.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                So use the debugger to find out! Start by breakpointing the line itself:

                total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);

                When it hits for the first time, the total and the loop index should both be zero because that is what your code told them to be:

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

                After it, you will have added the cell value, so step over that line. What's the value of the total now? If it's still zero, then it means that this code evaluates to zero:

                Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value)

                So look at why that is: what's the value of dataGridView1.Rows[i].Cells[4].Value - use the debugger to find out by firstly hovering over word "Value" and if that looks "right" then adding the calculation to teh Watch pane.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                A Offline
                A Offline
                ago2486
                wrote on last edited by
                #7

                when I put the breakpoint on total buy + = Convert.ToDecimal (dataGridView1.Rows [i] .Cells [4] .Value); total_achat displays 0.

                OriginalGriffO 1 Reply Last reply
                0
                • A ago2486

                  when I put the breakpoint on total buy + = Convert.ToDecimal (dataGridView1.Rows [i] .Cells [4] .Value); total_achat displays 0.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Yes, because the line hasn't been executed yet. Breakpoints stop at the beginning of the line, not the end. Step over the line, (as opposed to step into) and see if it changes.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  A 2 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Yes, because the line hasn't been executed yet. Breakpoints stop at the beginning of the line, not the end. Step over the line, (as opposed to step into) and see if it changes.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    A Offline
                    A Offline
                    ago2486
                    wrote on last edited by
                    #9

                    sir sorry and thank you for seeing my error after a review of my code.
                    I don't know how to explain it to you but I will illustrate it as a comment in my code.

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Yes, because the line hasn't been executed yet. Breakpoints stop at the beginning of the line, not the end. Step over the line, (as opposed to step into) and see if it changes.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      A Offline
                      A Offline
                      ago2486
                      wrote on last edited by
                      #10

                      private void Ajouter (String refProd, String quantite, String design, String prixU, String prixTtc)
                      {

                              String\[\] row = { refProd, quantite, design, prixU, prixTtc };
                              ListViewItem item = new ListViewItem(row);
                      
                              listView1.Items.Add(item);
                          }
                      

                      private void BtnAjouter_Click(object sender, EventArgs e)
                      {
                      double total;
                      decimal total_achat;

                              if (Lbl\_Affich\_Designation.Text == "" || Lbl\_Affich\_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl\_affich\_TxtQteStock.Text == "" || CmbRef\_Produit.Text == "")
                              {
                                  MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                              }
                              else
                              {
                                 
                                  if (CmbRef\_Produit.Text != "")
                                  {
                                     
                                      double a = 0;
                                      double b = 0;
                                      double c;
                                      int nbprod;
                               
                                      double.TryParse(TxtQteCmd.Text.Trim(), out a);
                                      double.TryParse(Lbl\_affich\_TxtQteStock.Text.Trim(), out b);
                                      double.TryParse(Lbl\_Affich\_PrixUnitaire.Text.Trim(), out c);
                      
                                      if (a <= b)
                                      {
                                          total = a \* c;
                                          total\_achat = 0;
                                          nbprod = 0;
                                     
                                         
                      
                                          try
                                          {
                              
                                              
                      
                                              using (OleDbCommand cmd = d.sql\_con.CreateCommand())
                                              {
                                                  cmd.CommandText = "INSERT INTO Detail\_temp (ref\_det, qute\_det, Designation, Prix\_unitaire\_HT, Prix\_total\_HT) VALUES (@ref\_det,@qute\_det,@Designation,@Prix\_unitaire\_HT,@Prix\_total\_HT)";
                                                  cmd.Parameters.AddWithValue("@ref\_det", CmbRef\_Produit.Text);
                                                  cmd.Parameters.AddWithValue("@qute\_det", TxtQteCmd.Text);
                                                  cmd.Parameters.AddWithValue("@Designation", Lbl\_Affich\_Designation.Text);
                                                  cmd.Parameters.AddWithValue("@Prix\_unitaire\_HT", Lbl\_Affich\_PrixUnitaire.Text);
                                                  cmd.Parameters.AddWithValue("@Prix\_total\_HT", total);
                      
                      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