Reportviewer 使用动态数据集显示空白报告
Reportviewer shows blank report using dynamic dataset
我关注了这个博客,我已经能够在 ReportViewer 中成功地将动态数据集与 Tablix 绑定 - https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-dataset-dynamically-created-in-code-to-your-local-report-with-reportviewer/
代码:
private void button1_Click(object sender, EventArgs e)
{
//Check if both selection criteria are present
if (comboBox1.Text == "" || comboBox2.Text == "")
{
MessageBox.Show("Please select employee", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
/* Generate payslip */
//Get dept name and role
DataSet dsDept = PanktiLa("select r.roledesc, d.depname from emprole r INNER JOIN deptmaster d on r.DEPSRNO = d.srno where r.empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
//Get PF no and ESI no
//We assume 1 = PF & 2 = ESI in DocMaster (I would like to keep these two values permanent)
string pf = "-";
string esi = "-";
DataSet dsDocs = PanktiLa("select * from empdocs where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
if (dsDocs.Tables[0].Rows.Count > 0) //If there are records
foreach (DataRow dr in dsDocs.Tables[0].Rows) //Cycle through dataset to find doc numbers
{
if (dr["DOCSRNO"].ToString() == "1") //This is PF#
pf = dr["DOCDESC"].ToString();
if (dr["DOCSRNO"].ToString() == "2") //This is ESI#
esi = dr["DOCDESC"].ToString();
}
//Get bank ac no
string khatano = "-";
DataSet dsBank = PanktiLa("select ACNO from empbank where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
if (dsBank.Tables[0].Rows.Count > 0) //If there are records
khatano = dsBank.Tables[0].Rows[0]["ACNO"].ToString();
/*===========================================================================*/
//Populate earning & deduction datasets
//To hold earnings
DataTable dtE = new DataTable();
dtE.Columns.Add("SALNAME");
dtE.Columns.Add("AMT");
//To hold deductions
DataTable dtD = new DataTable();
dtD.Columns.Add("SALNAME");
dtD.Columns.Add("AMT");
//Get all salary heads for the employee-month
DataSet dsPYSalary = PanktiLa("select p.FKSRNO, p.SALCODE, p.AMT, s.SALNAME, s.SALTYPE from PYSalary p INNER JOIN SalaryMaster s ON s.SALCODE = p.SALCODE where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
//Segregate and label them based on earnings and deductions
foreach (DataRow dr in dsPYSalary.Tables[0].Rows)
{
if (dr["SALTYPE"].ToString() == "Earning") //Head is of earning type
{
//Populate earning dataset 'dsE'
DataRow drE = dtE.NewRow();
drE["SALNAME"] = dr["SALNAME"];
drE["AMT"] = dr["AMT"];
//dsE.Tables[0].Rows.Add(drE);
dtE.Rows.Add(drE);
}
else if (dr["SALTYPE"].ToString() == "Deduction") //Head is of deduction type
{
//Populate deduction dataset 'dsD'
DataRow drD = dtD.NewRow();
drD["SALNAME"] = dr["SALNAME"];
drD["AMT"] = dr["AMT"];
//dsD.Tables[0].Rows.Add(drD);
dtD.Rows.Add(drD);
}
}
//Populate OT
//Get data
DataSet dsPYOT = PanktiLa("select p.FKSRNO, p.OTSRNO, p.OQTY, p.OTAMT, m.OTID, m.OTNAME from PYOT p INNER JOIN OTMaster m ON p.OTSRNO = m.SRNO where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
foreach (DataRow dr in dsPYOT.Tables[0].Rows)
{
//Populate earning dataset 'dsE'
DataRow drO = dtE.NewRow();
drO["SALNAME"] = dr["OTNAME"];
drO["AMT"] = dr["OTAMT"];
dtE.Rows.Add(drO);
}
//Add table to dataset
DataSet dsE = new DataSet();
dsE.Tables.Add(dtE);
DataSet dsD = new DataSet();
dsD.Tables.Add(dtD);
//Bind datasources
dsE.Tables[0].TableName = "dtEarnings";
this.dtEarningsBindingSource.DataSource = dsE;
dsD.Tables[0].TableName = "dtDeductions";
this.dtDeductionsBindingSource.DataSource = dsD;
/*===========================================================================*/
//Define parameters
ReportParameter _rpMonth = new ReportParameter("rpMonth", comboBox2.Text);
ReportParameter _rpName = new ReportParameter("rpName", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPNAME"].ToString());
ReportParameter _rpID = new ReportParameter("rpEmpID", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPID"].ToString());
ReportParameter _rpDept = new ReportParameter("rpDept", dsDept.Tables[0].Rows[0]["DEPNAME"].ToString());
ReportParameter _rpRole = new ReportParameter("rpRole", dsDept.Tables[0].Rows[0]["ROLEDESC"].ToString());
ReportParameter _rpPF = new ReportParameter("rpPFNO", pf);
ReportParameter _rpESI = new ReportParameter("rpESINO", esi);
ReportParameter _rpBankAcNo = new ReportParameter("rpBankAcNo", khatano);
//Set parameters
reportViewer1.LocalReport.SetParameters(new ReportParameter[]
{ _rpMonth, _rpName, _rpID, _rpDept, _rpRole, _rpPF, _rpESI, _rpBankAcNo });
//Refresh report
reportViewer1.RefreshReport();
}
private DataSet PanktiLa(string queryle)
{
string connString = System.IO.File.ReadAllText("D:\Payroll\Payroll.txt") + " User ID = sa; Password = DEMO";
DataSet ds_local = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(queryle, conn);
SqlDataAdapter adapter = new SqlDataAdapter();
conn.Open();
adapter.SelectCommand = cmd;
adapter.Fill(ds_local);
return (ds_local);
}
}
Tablix 甚至生成数据集中存在的行数。但它不会用数据集中的数据填充行。
我错过了什么?
注意: 我注意到虽然 DataSet
绑定到 dataTable
并依次绑定到 ReportViewer
,但没有一对一的绑定在 ReportViewer
的 DataSet Columns
和 RDLC Report
的 DataSet Columns
之间。那里有什么可以做的吗?
我在 C# 中使用 WinForms 进行编码,使用 VS2015 以及 SQL Server 2008 R2 Express 和 .Net 4.5.2
问题已解决。数据表的字段必须与代码中为数据集定义的列同名。
我关注了这个博客,我已经能够在 ReportViewer 中成功地将动态数据集与 Tablix 绑定 - https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-dataset-dynamically-created-in-code-to-your-local-report-with-reportviewer/
代码:
private void button1_Click(object sender, EventArgs e)
{
//Check if both selection criteria are present
if (comboBox1.Text == "" || comboBox2.Text == "")
{
MessageBox.Show("Please select employee", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
/* Generate payslip */
//Get dept name and role
DataSet dsDept = PanktiLa("select r.roledesc, d.depname from emprole r INNER JOIN deptmaster d on r.DEPSRNO = d.srno where r.empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
//Get PF no and ESI no
//We assume 1 = PF & 2 = ESI in DocMaster (I would like to keep these two values permanent)
string pf = "-";
string esi = "-";
DataSet dsDocs = PanktiLa("select * from empdocs where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
if (dsDocs.Tables[0].Rows.Count > 0) //If there are records
foreach (DataRow dr in dsDocs.Tables[0].Rows) //Cycle through dataset to find doc numbers
{
if (dr["DOCSRNO"].ToString() == "1") //This is PF#
pf = dr["DOCDESC"].ToString();
if (dr["DOCSRNO"].ToString() == "2") //This is ESI#
esi = dr["DOCDESC"].ToString();
}
//Get bank ac no
string khatano = "-";
DataSet dsBank = PanktiLa("select ACNO from empbank where empsrno = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
if (dsBank.Tables[0].Rows.Count > 0) //If there are records
khatano = dsBank.Tables[0].Rows[0]["ACNO"].ToString();
/*===========================================================================*/
//Populate earning & deduction datasets
//To hold earnings
DataTable dtE = new DataTable();
dtE.Columns.Add("SALNAME");
dtE.Columns.Add("AMT");
//To hold deductions
DataTable dtD = new DataTable();
dtD.Columns.Add("SALNAME");
dtD.Columns.Add("AMT");
//Get all salary heads for the employee-month
DataSet dsPYSalary = PanktiLa("select p.FKSRNO, p.SALCODE, p.AMT, s.SALNAME, s.SALTYPE from PYSalary p INNER JOIN SalaryMaster s ON s.SALCODE = p.SALCODE where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
//Segregate and label them based on earnings and deductions
foreach (DataRow dr in dsPYSalary.Tables[0].Rows)
{
if (dr["SALTYPE"].ToString() == "Earning") //Head is of earning type
{
//Populate earning dataset 'dsE'
DataRow drE = dtE.NewRow();
drE["SALNAME"] = dr["SALNAME"];
drE["AMT"] = dr["AMT"];
//dsE.Tables[0].Rows.Add(drE);
dtE.Rows.Add(drE);
}
else if (dr["SALTYPE"].ToString() == "Deduction") //Head is of deduction type
{
//Populate deduction dataset 'dsD'
DataRow drD = dtD.NewRow();
drD["SALNAME"] = dr["SALNAME"];
drD["AMT"] = dr["AMT"];
//dsD.Tables[0].Rows.Add(drD);
dtD.Rows.Add(drD);
}
}
//Populate OT
//Get data
DataSet dsPYOT = PanktiLa("select p.FKSRNO, p.OTSRNO, p.OQTY, p.OTAMT, m.OTID, m.OTNAME from PYOT p INNER JOIN OTMaster m ON p.OTSRNO = m.SRNO where FKSRNO = " + Convert.ToInt32(ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["SRNO"]));
foreach (DataRow dr in dsPYOT.Tables[0].Rows)
{
//Populate earning dataset 'dsE'
DataRow drO = dtE.NewRow();
drO["SALNAME"] = dr["OTNAME"];
drO["AMT"] = dr["OTAMT"];
dtE.Rows.Add(drO);
}
//Add table to dataset
DataSet dsE = new DataSet();
dsE.Tables.Add(dtE);
DataSet dsD = new DataSet();
dsD.Tables.Add(dtD);
//Bind datasources
dsE.Tables[0].TableName = "dtEarnings";
this.dtEarningsBindingSource.DataSource = dsE;
dsD.Tables[0].TableName = "dtDeductions";
this.dtDeductionsBindingSource.DataSource = dsD;
/*===========================================================================*/
//Define parameters
ReportParameter _rpMonth = new ReportParameter("rpMonth", comboBox2.Text);
ReportParameter _rpName = new ReportParameter("rpName", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPNAME"].ToString());
ReportParameter _rpID = new ReportParameter("rpEmpID", ds_combo1.Tables[0].Rows[comboBox1.SelectedIndex]["EMPID"].ToString());
ReportParameter _rpDept = new ReportParameter("rpDept", dsDept.Tables[0].Rows[0]["DEPNAME"].ToString());
ReportParameter _rpRole = new ReportParameter("rpRole", dsDept.Tables[0].Rows[0]["ROLEDESC"].ToString());
ReportParameter _rpPF = new ReportParameter("rpPFNO", pf);
ReportParameter _rpESI = new ReportParameter("rpESINO", esi);
ReportParameter _rpBankAcNo = new ReportParameter("rpBankAcNo", khatano);
//Set parameters
reportViewer1.LocalReport.SetParameters(new ReportParameter[]
{ _rpMonth, _rpName, _rpID, _rpDept, _rpRole, _rpPF, _rpESI, _rpBankAcNo });
//Refresh report
reportViewer1.RefreshReport();
}
private DataSet PanktiLa(string queryle)
{
string connString = System.IO.File.ReadAllText("D:\Payroll\Payroll.txt") + " User ID = sa; Password = DEMO";
DataSet ds_local = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(queryle, conn);
SqlDataAdapter adapter = new SqlDataAdapter();
conn.Open();
adapter.SelectCommand = cmd;
adapter.Fill(ds_local);
return (ds_local);
}
}
Tablix 甚至生成数据集中存在的行数。但它不会用数据集中的数据填充行。
我错过了什么?
注意: 我注意到虽然 DataSet
绑定到 dataTable
并依次绑定到 ReportViewer
,但没有一对一的绑定在 ReportViewer
的 DataSet Columns
和 RDLC Report
的 DataSet Columns
之间。那里有什么可以做的吗?
我在 C# 中使用 WinForms 进行编码,使用 VS2015 以及 SQL Server 2008 R2 Express 和 .Net 4.5.2
问题已解决。数据表的字段必须与代码中为数据集定义的列同名。