首页 > 编程语言 >C# winform中RDLC报表绘制

C# winform中RDLC报表绘制

时间:2024-06-12 21:32:23浏览次数:23  
标签:报表 C# RDLC reportViewer1 Add dataRow table winform string

C# winform中RDLC报表绘制

使用集成开发环境为VS2010,框架版本为.NET Framework4

以下我们以一个简单的学生报表作为例子。

public class Student
{
public string name{get;set;}
public string id{get;set;}
public string classes{get;set;}
public string home{get;set;}
public string imgPath{get;set;}
}

1.创建reportViewer

首先创建reportViewer来放置报表,选中工具栏的中reportviewer工具,然后拖入窗体中,选择在父容器中停靠
image

这次再选择新建项,创建数据集xsd,放入一个DataTable,取名为studentDataTable.
(数据集其他选择根据自己项目来),然后根据上面的Student类来添加列。
image

2.创建报表

接下来开始创建新报表,新建项中创建一个新报表,然后点击左上方新建,把上面的数据集设置为报表的数据集,名称为DataSet1
点击工具箱,拖一个表过来。点击表格右上面一个小矩形,选择对应的数据集数据进去。
点击图像,拖入表格中,选择外部中数据集属性。
image
依次设计好后,表格如下:
image
然后再返回窗体中的reportViewer中,选择为我们刚刚创建的报表,然后我们来运行此窗体,发现未显示,我们修改后台C#代码。
image

3.给报表传值和显示图像

创建学生数据

Student s = new Student();
            s.name = "张三";
            s.id = "001";
            s.home = "地球中国";
            s.classes = "计算机软件班";
            s.imgPath = @"file:\C:\Users\29749\Desktop\man1.png";

注意如果想要让报表中显示本地路径下的图片,前面需要加上file:\

创建DataSetDataTable数据集

 DataSet dataSet = new DataSet();
            DataTable table = new DataTable();
            table.Columns.Add("name", typeof(string));
            table.Columns.Add("id", typeof(string));
            table.Columns.Add("home", typeof(string));
            table.Columns.Add("classes", typeof(string));
            table.Columns.Add("imgPath", typeof(string));
            DataRow dataRow = table.NewRow();
            dataRow["name"] = s.name;
            dataRow["id"] = s.id;
            dataRow["home"] = s.home;
            dataRow["classes"] = s.classes;
            dataRow["imgPath"] = s.imgPath;
            table.Rows.Add(dataRow);
            dataSet.Tables.Add(table);

设置reportViewer属性

 //计算报表路径
            string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(
              0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            reportPath += @"\Report1.rdlc";
            //图像显示必须设置
            this.reportViewer1.LocalReport.ReportPath = reportPath;
            this.reportViewer1.LocalReport.EnableExternalImages = true;
            reportViewer1.LocalReport.DataSources.Clear();
            //这里DataSet1必须和报表中数据集名称一样
            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dataSet.Tables[0]));
            this.reportViewer1.RefreshReport();

以上代码编写后,在运行窗体程序后效果如下:
image

4.报表参数的使用

如果需要在报表中再添加额外的数据,这些数据不来自于数据集,你需要添加参数。
在左边列表中右键添加报表参数,然后在需要的文本框中就可以设置内容为报表参数了。

在C#的后端中可以编写报表参数,代码如下:

//必须和参数名称一样
ReportParameter rp = new ReportParameter("img", imgPath);
            this.reportViewer1.LocalReport.SetParameters(rp);
ReportParameter rp2 = new ReportParameter("time", this.date);
            this.reportViewer1.LocalReport.SetParameters(rp2);

这样报表参数就传递过去了

标签:报表,C#,RDLC,reportViewer1,Add,dataRow,table,winform,string
From: https://www.cnblogs.com/AndreaDO/p/18244059

相关文章

  • 基于imx6ull_pro中qtcreator环境搭建
    目录(一)说明(二)qt_creator安装(三)qt_creator配置与环境搭建(四)qt_creator所遇问题(一)说明1使用版本Descriptionubuntu18.04.2LTSRelease......
  • 【ARM Coresight Debug 系列 -- ARMv8/v9 Watchpoint 软件实现地址监控详细介绍】
    请阅读【嵌入式开发学习必备专栏】文章目录ARMv8/v9WatchpointexceptionsWatchpoint配置信息读取ExecutionconditionsWatchpointdataaddresscomparisonsSizeofthedataaccessWatchpoint软件配置流程WatchpointType使用介绍WT,Bit[20]:WatchpointType......
  • 论文解读——CVPR2024《Learning by Correction: Efficient Tuning Task for Zero-Sho
    一、研究背景  视觉-语言模型是一类能够处理和理解图像及其相关文本信息的模型,它们在多种视觉-语言任务中展示了卓越的性能。这些任务包括图像描述(imagecaptioning)、视觉问题回答(visualquestionanswering)、图像-文本检索(image-textretrieval)等。这些模型通常经过大规......
  • 论文解读——AAMAS2024《OPEx: A Large Language Model-Powered Framework for Embodi
    一、研究背景  具身指令执行(EmbodiedInstructionFollowing,EIF)是指在一个特定的物理或虚拟环境中,使能自主代理(如机器人或虚拟代理)根据自然语言指令来执行复杂的任务。这种研究领域集中于探索自然语言理解与机器执行能力的结合,尤其是在模拟家庭或日常环境中,如何使代理......
  • SpringMVC
    文章目录1.MVC架构2.基于 Servlet 的MVC模式3.Model1模式与Model2模式Model1模式:Model2模式:4.MVVM架构5.SpringMVC6.SpringMVC实现步骤8.Controller的两种实现方式9.RestFul风格10.@ResponseBody11.乱码解决12.过滤器VS拦截器区别:参考......
  • 初阶C语言(01)—学习笔记
    if语言if语句其一C语言被称为结构化的程序设计语言,包括顺序结构、选择结构(ifswitch)和循环结构(for while dowhile)。因为今天要下雨,所以必须带伞。这就是一个简单的选择语句。例如:如果你的年龄大于18岁,那么输出成年。#include<stdio.h>intmain(){ intage=20;......
  • golang sync.Map 与使用普通的 map 的区别
     使用sync.Map与普通的Gomap主要有以下几点区别:1.并发安全性普通map:在没有外部同步的情况下,不是并发安全的。在多goroutine访问时,如果没有适当的锁或其他同步机制保护,可能会导致数据竞争和未定义行为。sync.Map:是并发安全的。它内部实现了必要的同步机制,允许多......
  • 为什么在NLP中迟迟没有出现类似CV预训练的范式
    Q:2018年前,迁移学习在NLP中的运用情况如何?我们知道,直到2018年,ULM-FiT、GPT和BERT模型的出现才开启了NLP预训练模型的时代,才真正实现了CV领域那样的迁移学习方法在NLP领域的应用。那么,是不是说2018年前NLP领域就没有迁移学习呢?答案是,这个说法是非常不准确的!就如我们在6.4.3里预......
  • 基于phpstudy的Pikachu靶场搭建(有手就会)
    目录一、phpstudy和pikachu源码下载地址二、phpstudy创建网站,网站根目录设置为pikachu的源码文件夹1、将pikachu_master(pikachu源码文件夹)放到phpstudy的WWW目录下2、打开phpstudy,创建pikachu靶场网站三、配置pikachu靶场四、用phpstudy的mysql连接pikachu靶场的数据库......
  • Mamba: Linear-Time Sequence Modeling with Selective State Spaces
    目录概Mamba代码GuA.andDaoT.Mamba:Linear-timesequencemodelingwithselectivestatespaces.2023.概Mamba.MambaS4和S4D虽然解决了SSM计算速度的问题,但是有一个前提,就是\(A,B,C,D\)是与时间\(t\)无关的.这导致这些方法只能采取一种固定的模......