How to transfer content from a datagridview to report with reportviewer? C# windows form

Question:

Good morning, I am working on a school assignment, and I have to pass some data from a form to a report in reportviewer, I have managed to pass the data from some labels and textbox correctly, but when I want to pass the data from my datagridview, it only shows one of the rows in the report, specifically the last row, let's say I have 5 rows and only number 5 is shown, while the previous 4 are not shown. If anyone knows a solution and could explain it to me I would greatly appreciate it.

This is my main form code

TicketDeVenta tick = new TicketDeVenta();

  Datos dat = new Datos();
  dat.Expendio = "Local Principal";
  dat.Fecha = DateTime.Now.ToLongDateString();
  dat.Cliente = "Publico en general";
  dat.IVA = "Incluido en el precio";
  dat.Total = TotalApagarOP2.Text;
  dat.Cambio = CambioDelEfectivoOP.Text;

  for(int i = 0; i < dataGridView1.Rows.Count; i ++)
  {
    dat.Cantidad = (double)Convert.ToDouble(dataGridView1.Rows[i].Cells[0].Value);
    dat.Nombre = (string)dataGridView1.Rows[i].Cells[1].Value;
    dat.Marca = (string)dataGridView1.Rows[i].Cells[2].Value;
    dat.Precio = (string)(dataGridView1.Rows[i].Cells[3].Value);

   }
    tick.Datos.Add(dat);
    tick.Show();

This is the code of my Data class

public class Datos
{
    public string Expendio { get; set;}
    public string Fecha { get; set; }
    public string Cliente { get; set; }
    public string IVA { get; set; }
    public string Total { get; set; }
    public string Cambio { get; set; }

    //del grid
    public double Cantidad { get; set; }
    public string Nombre { get; set; }
    public string Marca { get; set; }
    public string Precio { get; set; }
}

My report code

 private void reportViewer1_Load(object sender, EventArgs e)
    {
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", Datos));
        this.reportViewer1.RefreshReport();

    }

Answer:

You'll need to separate your class first, since like this you now always store the latest data.

 public class Datos
    {
        public string Expendio { get; set;}
        public string Fecha { get; set; }
        public string Cliente { get; set; }
        public string IVA { get; set; }
        public string Total { get; set; }
        public string Cambio { get; set; }
        public List<Info>info{get;set}//Creas una lista para almacenar los demas datos
    }
    public class Info//Creas una nueva clase
       {
            public double Cantidad { get; set; }
            public string Nombre { get; set; }
            public string Marca { get; set; }
            public string Precio { get; set; }
      }

Second in your main code you will need to modify to something like this for the list to store the data.

TicketDeVenta tick = new TicketDeVenta();

  Datos dat = new Datos();
  dat.Expendio = "Local Principal";
  dat.Fecha = DateTime.Now.ToLongDateString();
  dat.Cliente = "Publico en general";
  dat.IVA = "Incluido en el precio";
  dat.Total = TotalApagarOP2.Text;
  dat.Cambio = CambioDelEfectivoOP.Text;
  dat.Info = new public List<Info>();//Creas una nueva instancia de la clase

  for(int i = 0; i < dataGridView1.Rows.Count; i ++)
  {
    Info info=new Info();//Creas una instancia
    info.Cantidad = (double)Convert.ToDouble(dataGridView1.Rows[i].Cells[0].Value);
    info.Nombre = (string)dataGridView1.Rows[i].Cells[1].Value;
    info.Marca = (string)dataGridView1.Rows[i].Cells[2].Value;
    info.Precio = (string)(dataGridView1.Rows[i].Cells[3].Value);
    dat.Info.Add(info);//Almacenas en la lista que creamos.
   }
    tick.Datos.Add(dat);
    tick.Show();

I hope it helps you.-

Scroll to Top