开发者

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

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开发者_StackOverflow中文版 do this.how can add the values in crystal report dynamically


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜