问题
How to Pass data table from DataGridView ro ReportViewer ?
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)
now I want to create a report with ReportViewr to show only checked rows in my 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 { }
}
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();
}
回答1:
Try something like the following,
ReportDataSource Rds = new ReportDataSource(dataSetName, bs);
reportViewer1.LocalReport.DataSources.Add(Rds);
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.
If you for some reason need to take the data directly from the DataGridView just replace bs
with dataGridView1.DataSource.
回答2:
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);
}
}
}
来源:https://stackoverflow.com/questions/23670958/how-to-pass-data-table-from-datagridview-to-reportviewer