Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法解决这个错误;无法为数据集';创建数据读取器;数据集1';_C# - Fatal编程技术网

C# 有没有办法解决这个错误;无法为数据集';创建数据读取器;数据集1';

C# 有没有办法解决这个错误;无法为数据集';创建数据读取器;数据集1';,c#,C#,我正在使用报表查看器打印报表。我遵循了一个关于 这段代码,但我得到了这个错误“无法为数据集'Dataset1'创建数据读取器” 我在这里使用reportviewer和rdlc。 我不知道问题是Reportdatasource本身还是数据集: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste

我正在使用报表查看器打印报表。我遵循了一个关于 这段代码,但我得到了这个错误“无法为数据集'Dataset1'创建数据读取器”

我在这里使用reportviewer和rdlc。 我不知道问题是Reportdatasource本身还是数据集:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using MySql.Data.MySqlClient;

namespace Water_Quality_Monitoring
{
    public partial class FishpondForm : Form
    {

        Microsoft.Reporting.WinForms.ReportDataSource rs = new 
Microsoft.Reporting.WinForms.ReportDataSource();
        private DataGridView grid;

        public FishpondForm()
        {
            InitializeComponent();
        }

private void btnPrint_Click(object sender, EventArgs e)
        {
            List<Print> list = new List<Print>();
            list.Clear();

            for (int i = 0; i < agriInfo.dataAgriInfo.Rows.Count - 1; i++)
            {
                list.Add(new Print
                {
                  MemberID = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  LastName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  FirstName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  MiddleName = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  Contact = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString(),
                  Status = 
                  agriInfo.dataAgriInfo.Rows[i].Cells[0].Value.ToString()
                });
                Microsoft.Reporting.WinForms.ReportDataSource rs = new 
                Microsoft.Reporting.WinForms.ReportDataSource();
                rs.Name = "Dataset1";
                rs.Value = list;
                PrintFishpond frm = new PrintFishpond();
                frm.reportViewer1.LocalReport.DataSources.Clear();
                frm.reportViewer1.LocalReport.DataSources.Add(rs);
                frm.reportViewer1.LocalReport.ReportEmbeddedResource = 
                "Water_Quality_Monitoring.Report1.rdlc";
                frm.ShowDialog();
            }
        }
    }

    public class Print
    {
        public string MemberID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string Contact { get; set; }
        public string Status { get; set; }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
Net系统;
使用MySql.Data.MySqlClient;
名称空间水质监测
{
公共部分类FishpondForm:表单
{
Microsoft.Reporting.WinForms.ReportDataSource rs=新建
Microsoft.Reporting.WinForms.ReportDataSource();
私有DataGridView网格;
公共鱼塘形式()
{
初始化组件();
}
私有void btnPrint\u单击(对象发送方,事件参数e)
{
列表=新列表();
list.Clear();
对于(int i=0;i

我希望打印datagrid dataAgriInfo中的内容。希望您能帮助我。

尝试将列表放入datatable中,并将datatable分配给ReportDataSource的value属性。使用本文中的“剪切+粘贴”功能:

然后

rs.Value = table;

您必须从datagrid中创建一个数据表,然后将其分配给报表数据源,如下所示:

DataTable dt = new DataTable();
dt = ((DataView)dataAgriInfo.ItemsSource).ToTable(); 
dt.TableName = "TableNameYouWantToSet" 

Microsoft.Reporting.WinForms.ReportDataSource rs = new 
                Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);  


frm.reportViewer1.LocalReport.DataSources.Clear();
frm.reportViewer1.LocalReport.DataSources.Add(rs);
frm.reportViewer1.RefreshReport();

错误为。“无法为数据集“Dataset1”创建数据读取器。它看起来像ReportDataSource。Value属性需要的是DataTable,而不是列表。可能的重复项无法访问ObjectReader。我在此计算机上前面没有VS,因此无法测试,但是,我发布了一个类似的SO帖子的链接,该帖子有一个可以剪切+粘贴并调用的函数,用于将列表转换为datatable。