how to create crystal report using datagridview va

2019-08-31 05:28发布

问题:

I am working Windows form Application in c#.I like to create crystal report from datagridview values instead of using database values. How can i do this,Is it possible to do this.how can add the values in crystal report dynamically

回答1:

You could create a DataSet and populate it with the values from the DataGridView. You can then bind the Crystal Report to the DataSet.

Something along the lines of:

DataSet ds = new DataSet();

ds = FetchDataFromGrid();

CrystalReport myReport = new CrystalReport()

myReport.SetDataSource(ds);

crystalReportViewer1.ReportSource = myReport

To retrieve the rows from a DataGridView you will need something like this:

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();


        foreach (DataGridViewRow  item in this.dataGridView1.Rows)
        {

            DataRow dr = dt.NewRow();

            if (item.DataBoundItem != null)
            {
                dr = (DataRow)((DataRowView)item.DataBoundItem).Row;
                dt.ImportRow(dr);
            }
        }

        ds.Tables.Add(dt);


回答2:

   SqlDataAdapter da = new SqlDataAdapter("select * from Sells", con.connection);
   da.SelectCommand.CommandType = CommandType.Text;
   DataSet ds = new DataSet();
   con.connection.Open();
   da.Fill(ds);
   con.connection.Close();
   TotalSellsReport rpt = new TotalSellsReport();
   rpt.SetDataSource(ds);
   SellsPrinting sp = new SellsPrinting();
   sp.crystalReportViewer1.ReportSource = rpt;
   sp.Show();