C# ReportViewer没有';不显示数据

C# ReportViewer没有';不显示数据,c#,.net,sql-server,parameters,reportviewer,C#,.net,Sql Server,Parameters,Reportviewer,其思想是输入新字段,然后在报告中显示它们 我过滤数据集中的表,它假设显示数据,它与DataGridView完美配合,现在我希望找到一种方法,用填充datagrid的相同数据集填充报表 public partial class bill : Form { SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\C#(Projects)\Nothin\billing

其思想是输入新字段,然后在报告中显示它们

我过滤数据集中的表,它假设显示数据,它与DataGridView完美配合,现在我希望找到一种方法,用填充datagrid的相同数据集填充报表

public partial class bill : Form
{
    SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\C#(Projects)\Nothin\billingSystem\billingSystem\Store.mdf;Integrated Security=True;User Instance=True");

    public bill(string Cusn,string su,string am,string to,string Di, string Cnum)
    {
        InitializeComponent();
        label1.Text = Cusn;
        label2.Text = su;
        label3.Text = am;
        label4.Text = to;
        label5.Text = Di;
        label6.Text = DateTime.Now.ToString("d/M/yyyy");
        label7.Text = Cnum;
    }

    private void bill_Load(object sender, EventArgs e)
    {
        LoadReport();
    }

    private void LoadReport()
    {

        int R = Convert.ToInt32(label7.Text);

        SqlDataAdapter ADAP = new SqlDataAdapter("Select * from Newbill where Con = '" + R + "'", cn);
        DataSet DS = new DataSet();
        ADAP.Fill(DS, "Store");
        dataGridView1.DataSource = DS.Tables["Store"];



        // TODO: This line of code loads data into the 'DataSet10.NewBill' table. You can move, or remove it, as needed.
        this.NewBillTableAdapter.Fill(this.DataSet10.NewBill, R);
        this.reportViewer1.RefreshReport();

        ReportParameter[] allPar = new ReportParameter[5]; // create parameters array
        ReportParameter parSu = new ReportParameter("Summation", label2.Text);
        ReportParameter parDiscount = new ReportParameter("Discount", label5.Text);
        ReportParameter parDisA = new ReportParameter("DiscountAmount", label3.Text);
        ReportParameter parTotal = new ReportParameter("Total", label4.Text);
        ReportParameter parCus = new ReportParameter("CustomerName", label1.Text);

        allPar[0] = parSu; //assign parameters to parameter array
        allPar[1] = parDiscount;
        allPar[2] = parTotal;
        allPar[3] = parDisA;
        allPar[4] = parCus;

        this.reportViewer1.LocalReport.SetParameters(allPar);
        this.reportViewer1.RefreshReport();
        // TODO: This line of code loads data into the 'DataSet1.NewBill' table. You can move, or remove it, as needed.
        //this.NewBillTableAdapter.Fill(this.DataSet1.NewBill, R, O);
        //this.reportViewer1.RefreshReport();
    }
}
我看到了:

实际上,我需要通过查询查看数据库中的数据,以过滤报告中的数据,我在google上找到的所有数据都是这样的:但当插入新字段时,它就不起作用了

如何,我尝试刷新数据集以包含表中发生的更改(在我的示例中,插入新行“新数据”) 但它给了我:

A data source instance has not been supplied for the data source 'DataSet 10_NewBill'.


private void LoadReport()
        {
            int R = Convert.ToInt32(label7.Text);

            this.reportViewer1.LocalReport.DataSources.Clear();
            DataTable dt = new DataTable();
            dt = this.NewBillTableAdapter.GetData(R);

            ReportDataSource rprtDTSource = new ReportDataSource(dt.TableName, dt);

            this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
            this.reportViewer1.RefreshReport(); 

            // TODO: This line of code loads data into the 'DataSet10.NewBill' table. You can move, or remove it, as needed.
            this.NewBillTableAdapter.Fill(this.DataSet10.NewBill,R );

            this.reportViewer1.RefreshReport();

            ReportParameter[] allPar = new ReportParameter[5]; // create parameters array
            ReportParameter parSu = new ReportParameter("Summation", label2.Text);
            ReportParameter parDiscount = new ReportParameter("Discount", label5.Text);
            ReportParameter parDisA = new ReportParameter("DiscountAmount", label3.Text);
            ReportParameter parTotal = new ReportParameter("Total", label4.Text);
            ReportParameter parCus = new ReportParameter("CustomerName", label1.Text);

            allPar[0] = parSu; //assign parameters to parameter array
            allPar[1] = parDiscount;
            allPar[2] = parTotal;
            allPar[3] = parDisA;
            allPar[4] = parCus;


            this.reportViewer1.LocalReport.SetParameters(allPar);
            this.reportViewer1.RefreshReport();
        }
还有这个

    int R = Convert.ToInt32(label7.Text);
    SqlDataAdapter ADAP = new SqlDataAdapter("Select * from Newbill where Con = '" + R + "'", cn);
    DataSet DS = new DataSet();
    ADAP.Fill(DS, "Store");
    DataTable dt = new DataTable();
    dt.TableName = "Store";
    cn.Open();
    ADAP.Fill(dt);
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    ReportDataSource source = new ReportDataSource("Store", dt);
    reportViewer1.LocalReport.DataSources.Clear();
    reportViewer1.LocalReport.DataSources.Add(source);
    reportViewer1.DataBind();
    reportViewer1.LocalReport.Refresh();
    cn.Close();
它告诉我:错误“Microsoft.Reporting.WinForms.ReportViewer”不包含“DataBind”的定义,并且找不到接受“Microsoft.Reporting.WinForms.ReportViewer”类型的第一个参数的扩展方法“DataBind”


那么我在使用时需要什么样的参考呢?

好吧,我将尝试回答您关于
WinForms
ASP.NET
的问题,因为您没有指定您使用的是哪个。问题是您正在对空报表调用
RefreshReport
。。。它返回的正是那个;空洞的报告

Win表单设置报告的数据源

//First Create a `DataSource` for your Report:

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   //This is what you are missing
this.reportViewer1.RefreshReport();
//First Create a `DataSource` for your Report - Same as winforms

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.DataBind(); //This is the difference in ASP.Net   
this.reportViewer1.RefreshReport();
ASP.Net设置报表的数据源

//First Create a `DataSource` for your Report:

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   //This is what you are missing
this.reportViewer1.RefreshReport();
//First Create a `DataSource` for your Report - Same as winforms

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.DataBind(); //This is the difference in ASP.Net   
this.reportViewer1.RefreshReport();

从您的示例中,您可以整天设置
SqlConnection
s、
ReportParameter
s和
DataTable
s,但是除非您将
ReportViewer
绑定到填充的
数据集
,否则您将一无所获

好的,我将尝试回答您关于
WinForms
ASP.NET
的问题,因为您没有指定您使用的是哪个。问题是您正在对空报表调用
RefreshReport
。。。它返回的正是那个;空洞的报告

Win表单设置报告的数据源

//First Create a `DataSource` for your Report:

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   //This is what you are missing
this.reportViewer1.RefreshReport();
//First Create a `DataSource` for your Report - Same as winforms

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.DataBind(); //This is the difference in ASP.Net   
this.reportViewer1.RefreshReport();
ASP.Net设置报表的数据源

//First Create a `DataSource` for your Report:

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   //This is what you are missing
this.reportViewer1.RefreshReport();
//First Create a `DataSource` for your Report - Same as winforms

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.DataBind(); //This is the difference in ASP.Net   
this.reportViewer1.RefreshReport();

从您的示例中,您可以整天设置
SqlConnection
s、
ReportParameter
s和
DataTable
s,但是除非您将
ReportViewer
绑定到填充的
数据集
,否则您将一无所获

这是我在将本地RDLC绑定到报表查看器时使用的

// create SqlConnection SqlConnection myConnection = new SqlConnection(ConnectionString); myCommand.Connection = myConnection; SqlDataAdapter da = new SqlDataAdapter(myCommand); //get the data DataSet data = new DataSet(); da.Fill(data); if (data != null && data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0) { ReportingServicesReportViewer.Visible = true; ltrStatus.Text = string.Empty; //provide local report information to viewer ReportingServicesReportViewer.LocalReport.ReportPath = Server.MapPath(Report.RDLCPath); //bind the report attributes and data to the reportviewer ReportDataSource rds = new ReportDataSource("DataSet1", data.Tables[0]); ReportingServicesReportViewer.LocalReport.DataSources.Clear(); ReportingServicesReportViewer.LocalReport.DataSources.Add(rds); ReportingServicesReportViewer.LocalReport.Refresh(); } else { ReportingServicesReportViewer.Visible = false; ltrStatus.Text = "No data to display."; } //创建SqlConnection SqlConnection myConnection=新的SqlConnection(ConnectionString); myCommand.Connection=myConnection; SqlDataAdapter da=新的SqlDataAdapter(myCommand); //获取数据 数据集数据=新数据集(); da.填写(数据); if(data!=null&&data.Tables.Count>0&&data.Tables[0]。Rows.Count>0) { ReportingServicesReportViewer.Visible=true; ltrStatus.Text=string.Empty; //向查看器提供本地报告信息 ReportingServicesReportViewer.LocalReport.ReportPath=Server.MapPath(Report.RDLCPath); //将报表属性和数据绑定到reportviewer ReportDataSource rds=新的ReportDataSource(“DataSet1”,data.Tables[0]); ReportingServicesReportViewer.LocalReport.DataSources.Clear(); ReportingServicesReportViewer.LocalReport.DataSources.Add(rds); ReportingServicesReportViewer.LocalReport.Refresh(); } 其他的 { ReportingServicesReportViewer.Visible=false; ltrStatus.Text=“无需显示的数据。”; }
我在没有给数据表命名时遇到了问题。您可以检查的另一件事是事件查看器是否存在报表查看器正在出现但未显示的异常?

这是我在将本地RDLC绑定到报表查看器时使用的方法

// create SqlConnection SqlConnection myConnection = new SqlConnection(ConnectionString); myCommand.Connection = myConnection; SqlDataAdapter da = new SqlDataAdapter(myCommand); //get the data DataSet data = new DataSet(); da.Fill(data); if (data != null && data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0) { ReportingServicesReportViewer.Visible = true; ltrStatus.Text = string.Empty; //provide local report information to viewer ReportingServicesReportViewer.LocalReport.ReportPath = Server.MapPath(Report.RDLCPath); //bind the report attributes and data to the reportviewer ReportDataSource rds = new ReportDataSource("DataSet1", data.Tables[0]); ReportingServicesReportViewer.LocalReport.DataSources.Clear(); ReportingServicesReportViewer.LocalReport.DataSources.Add(rds); ReportingServicesReportViewer.LocalReport.Refresh(); } else { ReportingServicesReportViewer.Visible = false; ltrStatus.Text = "No data to display."; } //创建SqlConnection SqlConnection myConnection=新的SqlConnection(ConnectionString); myCommand.Connection=myConnection; SqlDataAdapter da=新的SqlDataAdapter(myCommand); //获取数据 数据集数据=新数据集(); da.填写(数据); if(data!=null&&data.Tables.Count>0&&data.Tables[0]。Rows.Count>0) { ReportingServicesReportViewer.Visible=true; ltrStatus.Text=string.Empty; //向查看器提供本地报告信息 ReportingServicesReportViewer.LocalReport.ReportPath=Server.MapPath(Report.RDLCPath); //将报表属性和数据绑定到reportviewer ReportDataSource rds=新的ReportDataSource(“DataSet1”,data.Tables[0]); ReportingServicesReportViewer.LocalReport.DataSources.Clear(); ReportingServicesReportViewer.LocalReport.DataSources.Add(rds); ReportingServicesReportViewer.LocalReport.Refresh(); } 其他的 { ReportingServicesReportViewer.Visible=false; ltrStatus.Text=“无需显示的数据。”; }
我在没有给数据表命名时遇到了问题。您可以检查的另一件事是事件查看器是否存在报表查看器正在出现但未显示的异常情况?

首先创建rpt对象,然后设置数据源。
遵循以下步骤

CrystalReport1 objRpt = new CrystalReport1();  //create your rpt object
objRpt.SetDataSource(Dataset.Tables["tablename"]);   //set datasource to your rpt.
cryRptViewer.ReportSource = objRpt;  //give rpt object to your crystal report viewer.

首先创建rpt对象,然后设置数据源。
遵循以下步骤

CrystalReport1 objRpt = new CrystalReport1();  //create your rpt object
objRpt.SetDataSource(Dataset.Tables["tablename"]);   //set datasource to your rpt.
cryRptViewer.ReportSource = objRpt;  //give rpt object to your crystal report viewer.

问题在于数据表的名称(在代码中)与数据集的名称(在报表中)不匹配。您的错误给出了您应该使用的名称。试试这个:

private void LoadReport()
    {
        int R = Convert.ToInt32(label7.Text);

        this.reportViewer1.LocalReport.DataSources.Clear();
        DataTable dt = new DataTable();
        dt = this.NewBillTableAdapter.GetData(R);
        //the DataTable name MUST match the name of the corresponding dataset 
        //   (as it is named in the report)
        dt.TableName = "10_NewBill";

        ReportDataSource rprtDTSource = new ReportDataSource(dt.TableName, dt);

        this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
        this.reportViewer1.RefreshReport(); 

        //etc.

问题在于数据表的名称(在代码中)与数据集的名称(在报表中)不匹配。您的错误给出了您应该使用的名称。试试这个:

private void LoadReport()
    {
        int R = Convert.ToInt32(label7.Text);

        this.reportViewer1.LocalReport.DataSources.Clear();
        DataTable dt = new DataTable();
        dt = this.NewBillTableAdapter.GetData(R);
        //the DataTable name MUST match the name of the corresponding dataset 
        //   (as it is named in the report)
        dt.TableName = "10_NewBill";

        ReportDataSource rprtDTSource = new ReportDataSource(dt.TableName, dt);

        this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
        this.reportViewer1.RefreshReport(); 

        //etc.

你根本没看到报告吗?是空的吗?我看不到您将任何数据绑定到报表的任何地方。我创建了一个dataset10.xsd并拖动表,然后配置适配器并通过添加where Con=@ConSorry Maged来编辑查询。。。我在工作中做的编码之间回答这些问题,有时需要花点时间