none
Help with a ReportViewer

    Question

  • Hey there, i'm having a few problems making a report in Visual Studio 2015. i'm inserting my data in a datagridview with 2 fields, and it loads perfectly while i'm using "Queue" to load those fields. the thing is when I generate my report, if my DG have more than two rows it only shows the last row added into the datagrid. where im having the error ?

    Note: I'm not using a DB and I put in "Bold" where i think the error is. 

    Help me if u can, Regards 

            private void InvoiceGenerate()
            {
                //llenarla con los valores contenidos en los controles del Formulario
                if (a < 1)
                {
                    invoice.Fecha = Convert.ToDateTime(dateTimePicker_FECHA.Text);
                    invoice.Cliente = textBox_CLIENTE.Text;
                    invoice.ruc = textBox_RUC.Text;
                    invoice.departamento = textBox_DEPARTAMETO.Text;
                    invoice.dirigido = textBox_DIRIGIDO.Text;
                    invoice.direccion = textBox_DIRECCION.Text;
                    invoice.telefono = textBox_TELEFONO.Text;
                    invoice.Letras = textBox_TOTALL.Text;
                    invoice.Anexo = textBox_Anexo.Text;
                    invoice.Detalle = textBox_DETALLE.Text;
                    invoice.Servicio = textBox_Concepto.Text;
                    double YY = Sub + ITBMS;
                    invoice.SUB = float.Parse(Sub.ToString("N2"));
                    invoice.itbms = float.Parse(ITBMS.ToString("N2"));
                    invoice.total = float.Parse(YY.ToString("N2"));
                    if (radioButton_contado.Checked)
                    {
                        invoice.condicionco = "X";
                    }
                    else
                    {
                        if (radioButton_Credito.Checked)
                        {
                            invoice.condicioncr = maskedTextBox_DIAS.Text;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Solo se puede Generar una Factura a la vez");
                }
                a = a + 1;
                foreach (DataGridViewRow row in dataGridView_Concepto.Rows)
                {
                        article.Concepto = Convert.ToString(row.Cells[0].Value);
                        article.Monto = float.Parse(Convert.ToString(row.Cells[1].Value));MessageBox.Show(""+row);
                }
                frm.Detail.Add(article);
                frm.Invoice.Add(invoice);
                frm.Show();
            }
    Saturday, April 1, 2017 1:15 AM

Answers

  • Fouad RoumiehI did what you said, 

      foreach (DataGridViewRow row in dataGridView_Concepto.Rows)
                {
                    article.Concepto = Convert.ToString(row.Cells[0].Value);
                    article.Monto = float.Parse(Convert.ToString(row.Cells[1].Value));
                    frm.Detail.Add(article);
                }

    and this is what happend

    it only saves the last field, and i added G1 with 1000 and it appears as a duplicated G2


    Sorry, I noticed now that you not initializing the Article object inside the loop also, so you ended up adding the object and overwrite it in each iteration. Please check my updated code above.

    Fouad Roumieh

    • Marked as answer by Alex Alfonso Sunday, April 2, 2017 6:19 AM
    Sunday, April 2, 2017 4:18 AM

All replies

  • You are adding the object outside the loop, you should instantiate and populate the object then append it at the same time within the loop iteration:

    foreach (DataGridViewRow row in dataGridView_Concepto.Rows) {

    Article article = new Article(); article.Concepto = Convert.ToString(row.Cells[0].Value); article.Monto = float.Parse(Convert.ToString(row.Cells[1].Value));MessageBox.Show(""+row);

    frm.Detail.Add(article);

    }



    Fouad Roumieh





    Saturday, April 1, 2017 11:47 AM
  • Fouad RoumiehI did what you said, 

      foreach (DataGridViewRow row in dataGridView_Concepto.Rows)
                {
                    article.Concepto = Convert.ToString(row.Cells[0].Value);
                    article.Monto = float.Parse(Convert.ToString(row.Cells[1].Value));
                    frm.Detail.Add(article);
                }

    and this is what happend

    it only saves the last field, and i added G1 with 1000 and it appears as a duplicated G2


    Sunday, April 2, 2017 1:06 AM
  • Fouad RoumiehI did what you said, 

      foreach (DataGridViewRow row in dataGridView_Concepto.Rows)
                {
                    article.Concepto = Convert.ToString(row.Cells[0].Value);
                    article.Monto = float.Parse(Convert.ToString(row.Cells[1].Value));
                    frm.Detail.Add(article);
                }

    and this is what happend

    it only saves the last field, and i added G1 with 1000 and it appears as a duplicated G2


    Sorry, I noticed now that you not initializing the Article object inside the loop also, so you ended up adding the object and overwrite it in each iteration. Please check my updated code above.

    Fouad Roumieh

    • Marked as answer by Alex Alfonso Sunday, April 2, 2017 6:19 AM
    Sunday, April 2, 2017 4:18 AM
  • Fouad Roumieh Thanks for your help, just a simple thing was messing around. 

    Regards, GN

    Sunday, April 2, 2017 6:21 AM
  • Fouad Roumieh Thanks for your help, just a simple thing was messing around. 

    Regards, GN

    You welcom, please mark the correct post as answer ( the one proposed by Alberto) this will help others when visiting this question.

    Fouad Roumieh

    Sunday, April 2, 2017 7:59 AM