views:

51

answers:

1

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  A: 

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);
Barry
I can i fetch the data from grid
ratty
@barry Thanks you very much for your answer
ratty
@ratty - no problem
Barry
barry i got some error in that one column not allowed null exception
ratty
my first gird view column is null how can omit that column
ratty
@barry did u get correct answer for ur coding
ratty
@ratty - You could either set `EnforceConstraints` to false on the DataSet or replace the Null values with a valid value. Maybe you could either post some more details or ask a new question?
Barry
Why mark this as unanswered? I answered your question, and added more than you initially asked for.
Barry