如何将数据表从DataGridView传递到ReportViewer

时间:2014-05-15 06:39:50

标签: c# datagridview reportviewer

如何从DataGridView ro ReportViewer传递数据表?

在win窗体中我有一个从SqlDataAdapter填充的DataGridView控件,我向它添加了一个复选框列。 for"准备打印"(或像这样smt)

现在我想创建一个包含ReportViewr的报表,以便仅在我的DataGridView中显示已检查的行。

这里是我的form1的代码(包含datagridview)

private void Fill_DGV_Sefaresh()
    {
        try
        {
            MyDB db = new MyDB();//class that contains my SQL Connection
            string sql = "SELECT sid, CONVERT(VARCHAR(10), cast(sdate as date), 111)sdate, sdes, sfor, suname, isdone, sqty FROM sefaresh order by sid desc";
            SqlDataAdapter sda = new SqlDataAdapter(sql,db.MyConn);
            DataSet ds = new DataSet();
            BindingSource bs = new BindingSource();
            sda.Fill(ds);
            bs.DataSource = ds.Tables[0].DefaultView;
            bindingNavigator1.BindingSource = bs;
            dataGridView1.DataSource = bs;
        }
            catch { }
}
我的form2的

代码(包含ReportViewer控件)

private SqlDataAdapter idata()
    {
        MyDB db = new MyDB();
            string sql = "SELECT * FROM sefaresh";
            SqlDataAdapter sda = new SqlDataAdapter(sql, db.MyConn);
            return sda;
        }
private void SefareshView_Load(object sender, EventArgs e)
        {
            idata().Fill(this.DS_Sefaresh.sefaresh);//DS_Sefaresh is a dataset item , sefaresh is a Table adapter
            this.reportViewer1.RefreshReport();
        }

2 个答案:

答案 0 :(得分:0)

尝试以下内容,

ReportDataSource Rds = new ReportDataSource(dataSetName, bs);
reportViewer1.LocalReport.DataSources.Add(Rds);

其中bs是您的BindingSource,dataSetName是报表中数据集的名称,Rds是ReportViewer的数据源,reportViewer1是ReportViewerControl。

如果由于某种原因需要直接从DataGridView获取数据,只需将bs替换为dataGridView1.DataSource.

答案 1 :(得分:0)

Segue

    private void tbcaixas_Leave(object sender, EventArgs e)
    {
        Carga();
        int caixas = Convert.ToInt32(tbcaixas.Text);
        List<int> lista = new List<int>();
        for (int i = 1; i <= caixas; i++)
        {
            DataRow linha = dt.NewRow();
            linha["ncaixa"] = "1/" + i;
            linha["loja"] = tbLoja.Text.ToUpper();
            dt.Rows.Add(linha);           
        }
    }

    private void Carga()
    {
        dt = new DataTable();
        dt.Columns.Add("ncaixa", typeof(string));
        dt.Columns.Add("artigo", typeof(string));
        dt.Columns.Add("tam", typeof(string));
        dt.Columns.Add("cor", typeof(string));
        dt.Columns.Add("arte", typeof(string));
        dt.Columns.Add("qtd", typeof(string));
        dt.Columns.Add("und", typeof(string));
        dt.Columns.Add("loja", typeof(string));
        dgvDados.DataSource = dt;
    }

    private void btnImprimir_Click(object sender, EventArgs e)
    {
        DataTable dtImpressao = ((DataTable)dgvDados.DataSource);
        var parametro1 = new ReportParameter("data", dtpData.Text);
        var parametro2 = new ReportParameter("cliente", tbCliente.Text);
        var parametro3 = new ReportParameter("notafiscal", tbNotaFiscal.Text);
        var parametro4 = new ReportParameter("pesobruto", tbPesoBruto.Text);
        var parametro5 = new ReportParameter("pesoliquidado", tbPesoLiquidado.Text);
        var parametro6 = new ReportParameter("pesoliquidado", cbEmpresa.SelectedItem.ToString());
        ReportParameter[] paremetros = new ReportParameter[] { parametro1, parametro2, parametro3, parametro4, parametro5 };
        ImpressaoHelper.Imprimir(null, "DataSetRelatorios", "br.com.bonor.Core.relatorios.pcp.Romaneio.rdlc", dtImpressao);
    }
}

}