how to create crystal report using datagridview values without using database in C#?

妖精的绣舞 提交于 2019-12-25 08:39:02

问题


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();


来源:https://stackoverflow.com/questions/3360326/how-to-create-crystal-report-using-datagridview-values-without-using-database-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!