Reportviewer使用动态数据集显示空白报告 [英] Reportviewer shows blank report using dynamic dataset
问题描述
我已关注此博客,并且我已成功将动态数据集与ReportViewer中的Tablix绑定 - https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-数据集 - 动态创建代码到您的本地报告与报告查看器/
代码:
I have followed this blog and I have been able to successfully bind a dynamic dataset with a Tablix in a ReportViewer - https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-dataset-dynamically-created-in-code-to-your-local-report-with-reportviewer/
Code:
/*===========================================================================*/
//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甚至可以生成数据集中存在的行数。但它没有用数据集中的数据填充行。
我缺少什么?
我正在使用带有SQL Server 2008 R2 Express和.Net 4.5.2的VS2015在C#中使用WinForms进行编码。
我尝试过:
我尝试删除控件并将其添加回来..
The tablix even generates the number of rows present in the dataset. But it does not populate the rows with data in the dataset.
What am I missing?
I am coding using WinForms in C# using VS2015 with SQL Server 2008 R2 Express and .Net 4.5.2
What I have tried:
I tried deleting the controls and adding them back..
推荐答案
问题解决了。数据表的字段必须与代码中为数据集定义的列具有相同的名称。
Problem solved. The fields of the datatable must have the same names as the columns defined for the dataset in the code.
这篇关于Reportviewer使用动态数据集显示空白报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!