DataTable作为ReportViewer中的DataSource

本文关键字:DataSource 中的 ReportViewer 作为 DataTable | 更新日期: 2023-09-27 18:21:59

我正在做一个学校项目。但我需要在ReportViewer中将DataTable设置为DataSource。首先,用户键入文档的ID,然后单击按钮,按钮调用一个类,该类在我的数据库中进行选择并返回12个字段。我已经创建了一个包含选择结果的所有字段的数据集,并将其选择为报表数据源。但我需要将select的数据传输到DataSet,因为当我启动应用程序时,Report会出错。这是代码,我希望你能帮助我!谢谢

Buttton代码:

SelectDocumento doc = new SelectDocumento();
        doc.ImprimirDoc(int.Parse(txtID.Text));

在我的数据库中选择数据的类:

public void ImprimirDoc(int id)
    {
        string pesquisar = "CALL SP_Imprimir_Documento(" + id + ")";
        MySqlConnection con;
        con = new MySqlConnection("Persist Security Info=false; server=localhost; database=hospital; uid=root; pwd=");
        MySqlDataAdapter adapter = new MySqlDataAdapter(pesquisar, con);
        DataTable dt = new DataTable();
        dt.TableName = "DataSet1";
        con.Open();
        adapter.Fill(dt);
        ImprimirDocumento imprimir = new ImprimirDocumento(dt);
        imprimir.ShowDialog();
    }

报表代码:

private DataTable proc;
    public ImprimirDocumento(DataTable select)
    {
        InitializeComponent();
        proc = select;
    }
    ConexaoHospital bd = new ConexaoHospital();
    SelectDocumento doc = new SelectDocumento();
    private void ImprimirDocumento_Load(object sender, EventArgs e)
    {
        this.rptDocumento.RefreshReport();
        this.rptDocumento.LocalReport.DataSources.Clear();
        Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", proc);
        this.rptDocumento.LocalReport.DataSources.Add(rprtDTSource);
        this.rptDocumento.RefreshReport();
    }

报告显示的错误:

错误

DataTable作为ReportViewer中的DataSource

如果有人有类似的问题,我的帖子的这种方式是正确的,我的问题是因为我的ReportViewer数据集名称与我的DataTable不同。我的DataTable名称为"DataSet1",DataSet名称为"Documento"。我更改了"DataSet1"的数据集名称,它就起作用了。