0

我已经为datagridview(form1)中的每一行动态添加了复选框,并且我正在尝试仅针对选中复选框的那些行在新表单(form 2)上生成水晶报告。

我的 form1 按钮点击代码是

private void btn_Print_Click(object sender, EventArgs e)
{
//im trying to insert selected rows in datatable which is datasource for crystal report   
  DataTable table = new DataTable();
  int i = 1;
  foreach (DataGridViewRow row in dataGridView1.SelectedRows)
//dataGridView1.SelectedRows[0].Cells["select"].Value)
        {
            for (int j = 1; j < dataGridView1.ColumnCount; ++j)
            {
                table.Rows[i][j] = row.Cells[j].Value;
            }
            ++i;
        }
        if (cb_reptype.SelectedItem.ToString() == "Individual")
        {
    //here im specifying the path for new form2
          string path = table.ToString();//dataGridView1.SelectedRows[1].Cells["table"].Value.ToString();
            Form2 f2 = new Form2(path);
            //ReportDocument crystal = new ReportDocument();
            //crystal.Load(dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString());
            //pass = crystal;
            f2.Show();

        }

我在form2上的代码是

public partial class Form2 : Form
{
    public string source;
    public Form2(string path)
    {
        source = path;
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        this.crystalReportViewer1.ReportSource = source;
    }
}

在调试程序和按钮单击事件时,新表单正在打开,但显示以下错误。

我已经尝试了很多关于这个主题的研究,但没有达到标准。

请尽快回复..谢谢:)在此处输入图像描述

4

1 回答 1

0

尝试在按钮单击事件内的表单构造函数中提供有效文档。有效文档可以是.xml、.rpt 文件,用于生成水晶报表。

对于当前场景,做一些补充:

if (cb_reptype.SelectedItem.ToString() == "Individual")
{
  DataSet ds = new DataSet();
  ds.Tables.Add(table);
  ds.WriteXmlSchema("Drive:\\Somefolder\\Sample.xml"); // this generates Xml file.

现在将此路径传递给 Form 构造函数,因为这是有效的报告文件。

  Form f2 = new Form("Drive:\\Somefolder\\Sample.xml");
  f2.show();
}

在从 datagrid 添加任何值之前,您需要向 DataTable 添加行。

while(table.Rows.Count<dataGridView1.SelectedRows.Count)
{
  table.Rows.Add();
}

完成此操作后,您可以从 datagrid 添加值:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
  for (int j = 1; j < dataGridView1.ColumnCount; ++j)
  {
    table.Rows[i][j] = row.Cells[j].Value;
  }
  ++i;
}

希望它有效。

于 2013-08-23T20:46:09.080 回答