Add a listview cell
-
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();
-
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();
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!
-
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!
-
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!
-
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!
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);
-
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);
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!
-
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!
-
when I put the breakpoint on total buy + = Convert.ToDecimal (dataGridView1.Rows [i] .Cells [4] .Value); total_achat displays 0.
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!
-
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!
-
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!
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);