如何将数据表从DataGridView传递到ReportViewer [英] How to Pass data table from DataGridView to ReportViewer

查看:47
本文介绍了如何将数据表从DataGridView传递到ReportViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to Pass data table from DataGridView ro ReportViewer ?

在胜利表格中,我有一个从DataDataAdapter填充的DataGridView控件,并向其中添加了一个复选框列.用于准备打印"(或类似的短信)

in a win Form I have a DataGridView control that fills from a SqlDataAdapter , and I added a check box column to it. for "ready to print"(or smt like this)

现在,我想使用ReportViewr创建一个报表,以仅在DataGridView中显示选中的行.

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

here is codes of my form1 (Contains 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控件)

codes of my form2 (contains ReportViewer Control)

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

推荐答案

尝试如下操作,

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

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

Where bs is your BindingSource, dataSetName is the name of the dataset in the report, Rds is the datasource for the ReportViewer, and reportViewer1 is the ReportViewerControl.

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

If you for some reason need to take the data directly from the DataGridView just replace bs with dataGridView1.DataSource.

这篇关于如何将数据表从DataGridView传递到ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆