Dear Frien,
I am working in C# .... I nedd Help/.]
I created FROM1 which has DATAGRIDVIEW and fetched data from Database,and on the Form2 has Crystal Report Design , how can transfer data from Datagrideview of FORM1 to Crystal Report of FORM2..........
Code is bellow-:

private void btn_CrtRep_Click(object sender, EventArgs e)
        {

            {
                pur_DS ds = new pur_DS();
                int file = dataGridView1.Rows.Count;

                for (int i = 0; i <= file - 2; i++)
                {
                    ds.Tables[0].Rows.Add
                        (new object[]
                        {
                        dataGridView1[0,i].Value.ToString(),
                        dataGridView1[1,i].Value.ToString(),
                        dataGridView1[2,i].Value.ToString(),
                        dataGridView1[3,i].Value.ToString(),
                        dataGridView1[4,i].Value.ToString(),
                        dataGridView1[5,i].Value.ToString(),
                        dataGridView1[6,i].Value.ToString(),
                        dataGridView1[7,i].Value.ToString(),
                        dataGridView1[8,i].Value.ToString(),
                        dataGridView1[9,i].Value.ToString(),

                         });
                }
                form2 frm = new form2();
                ReportDocument crCR = new ReportDocument();
                crCR.Load("D:\\Personal\\MMS\\testCR\\testCR\\PurRep.rpt");
                crCR.SetDataSource(ds);
                frm.crystalReportViewer1.ReportSource = crCR;
                frm.crystalReportViewer1.Refresh();
                frm.Show();
            }

Please help me as soon as posible.

Dani AI

Generated

Short summary and focused fixes (for and others): the usual problems are (1) the report expects a specific schema and the runtime DataSet does not exactly match it, and (2) the DataGridView loop includes the "new row" or null cells. is right that tutorials help — but the practical fix is to push a DataTable whose column names and types match the report (prefer a typed DataSet / .xsd that the .rpt was designed against), populate it safely, then call ReportDocument.SetDataSource.

Example pattern (replace table/column names with your actual schema):

var ds = new pur_DS();                 // typed dataset used by the report
var dt = ds.Tables["PurTable"];        // or ds.Tables[0]
dataGridView1.EndEdit();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.IsNewRow) continue;
    var dr = dt.NewRow();
    dr["ItemCode"] = row.Cells[0].Value ?? DBNull.Value;
    dr["Qty"]      = row.Cells[1].Value ?? DBNull.Value;
    // fill other fields by column name
    dt.Rows.Add(dr);
}

using (var rpt = new ReportDocument())
{
    rpt.Load(Path.Combine(Application.StartupPath, "PurRep.rpt")); // avoid hard-coded D: path
    rpt.SetDataSource(ds);
    var frm = new form2();
    frm.crystalReportViewer1.ReportSource = rpt;
    frm.crystalReportViewer1.RefreshReport();
    frm.Show();
}

Troubleshooting notes: confirm ds.Tables[0].Rows.Count > 0 before SetDataSource; verify the report was designed against the same typed dataset (.xsd) and column names match exactly; if fields are missing or schema-mismatched use "Verify Database"/Set DataSource Location in the report designer; check exceptions and the report file path. If you still get no data, try binding rpt.SetDataSource(dt) instead of the whole DataSet to narrow the issue.

Recommended Answers

All 2 Replies

Anybody can help me plz

Who is Frien? Anyhow it appears you are posting widely. But searching seems to find a tutorial. So moving on.
http://www.c-sharpcorner.com/forums/transfer-data-from-dgv-of-form1-to-crystal-report-of-form2
https://www.youtube.com/watch?v=7J_zpg1Xo0A shows passing data from grid to another form.
notes how to get data into CR without a database.

You have a lot of stuff to read, and more work ahead of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.