说到报表打印、那就不得不说需要查数据库了,然后填写报表信息。设计报表用的 grid++。

查数据库时候,我也是醉了,直接一个表自身与自身级联了4次。。。一共取了7个表的信息数据。

关于级联--(表字段相同。但是  表1的父id可以作为表2的子id继续查询,循环递归。。。)

用了左级联解决了,你可以看做是一张DNA的表吧,子--父(母)--(外)祖父、(外)祖母--。。。。

 //级联查询

     select t1.BoarID,t1.R_Herd,t1.Sex,t1.BirthField,t1.BirthDate,t1.EarNo,t1.BirthFetus,t1.NippleOfLeft,t1.NippleOfRight,t1.R_LSize,t1.R_Coeff,t1.FatherID,t1.MotherID,
t2.FatherID as fFatherID,t2.MotherID as fMotherID,
t3.FatherID as ffFatherID,t3.MotherID as fmMotherID,
t4.FatherID as fffFatherID,t4.MotherID as fmmMotherID
m2.FatherID as MFatherID,m2.MotherID as MMotherID,
m3.FatherID as MfFatherID,m3.MotherID as MmMotherID,
m4.FatherID as MffFatherID,m4.MotherID as MmmMotherID
from [Breed].[dbo].[MY_Y_BoarBaseInfo] t1
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t2 on t1.FatherID = t2.BoarID
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t3 on t2.FatherID = t3.BoarID
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] t4 on t2.MotherID = t4.BoarID
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m2 on t1.MotherID = m2.BoarID
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m3 on m2.FatherID = m3.BoarID
left join [Breed].[dbo].[MY_Y_BoarBaseInfo] m4 on m2.MotherID = m4.BoarID
where t1.BoarID = '';

你在查询数据的时候,首先要在数据库写一个存储过程,查询时候,直接调存储过程即可,调用方法

        public DataTable selectDetailData(string str)
{
DataTable dt = new DataTable();
SqlParameter[] para = {
new SqlParameter("@BoarID",str)
};
string boarid = str; //string sqlStr = string.Format(("select FatherID,MotherID,R_Herd,EarNo,BirthDate,BirthField,NippleOfLeft,NippleOfRight,Sex,R_Count from [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = '{0}') and select FatherID as grandfatherID FROM [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = ( SELECT FatherID from [Breed].[dbo].[MY_Y_BoarBaseInfo] where BoarID = '{1}',"boarid,boarid); dt = DataBaseModule.ExecuteReturnDataTable(DataBaseModule.ConnectionString,
CommandType.StoredProcedure, "存储过程名字", para); return dt;
}

或者调用sql语句也可。。但是这就太。。。(注意根据使用方法选取类型)

(关于在vs上面配置信息,就自己配吧,config里面改下,连接数据库就行了)

    <add key="connection" value="server=服务器;database=数据库;user=用户名;password=密码;" />

一个 SQLHelper类帮助 连接数据库,一句话

 public static string ConnectionString88 = MySecurity.SDecryptString(ConfigurationManager.AppSettings.Get("connection88"));

报表设计时候、记得需要打印信息的字段填写。。。这个要自己摸索了,上下代码。

首先,报表打印的一个类。。我还没到这一步,直接拿的别人的。。不需要的删了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using grdesLib;
using grproLib; namespace GReport
{
public class GridReport
{ private List<string> _list; public List<string> list
{
set { _list = value; }
}
/// <summary>
/// 报表
/// </summary>
GridppReport Report = new GridppReport(); /// <summary>
/// 需要导出的datagridview
/// </summary>
public DataGridView dgv; public DataTable db;
public DataRow[] dr;
/// <summary>
/// 报表列
/// </summary>
public string[] ReportColumn;
/// <summary>
/// 报表参数
/// </summary>
public string[] ReportParameter;
/// <summary>
/// 设计程序参数
/// </summary>
public string[] ReportPParameter;
/// <summary>
/// 报表模版路径
/// </summary>
public string TemplatePath;
public object tempReport; /// <summary>
/// 关联参数
/// </summary>
public void Reportparameter()
{
if (ReportParameter.Length == ReportPParameter.Length)
{
for (int j = ; j < ReportParameter.Length; j++)
{ string str1 = ReportParameter[j];
string str2 = ReportPParameter[j];
Report.ParameterByName(ReportParameter[j]).AsString = ReportPParameter[j];
}
}
} /// <summary>
/// 填充列
/// </summary>
private void ReportFetchRecord()
{
if (dgv != null)
{
for (int row = ; row < dgv.RowCount; row++)
{
Report.DetailGrid.Recordset.Append();
for (int i = ; i < ReportColumn.Length; i++)
{
if (dgv.Rows[row].Cells[i].Value != null)
{
Report.FieldByName(ReportColumn[i]).AsString = dgv.Rows[row].Cells[i].Value.ToString();
//Report.FieldByName(ReportColumn[i]).AsString = dgv.Rows[row].Cells[ReportColumn[i]].Value.ToString();
}
}
Report.DetailGrid.Recordset.Post();
}
}
if (db != null)
{
for (int row = ; row < db.Rows.Count; row++)
{
Report.DetailGrid.Recordset.Append();
for (int i = ; i < ReportColumn.Length; i++)
{
if (db.Rows[row][i] != null)
{
Report.FieldByName(ReportColumn[i]).AsString = db.Rows[row][i].ToString();
string str = db.Rows[row][i].ToString();
}
}
Report.DetailGrid.Recordset.Post();
}
}
if (dr != null && dr.Length > )
{
for (int row = ; row < dr.Length; row++)
{
Report.DetailGrid.Recordset.Append();
for (int i = ; i < ReportColumn.Length; i++)
{
if (dr[row][i] != null)
{
Report.FieldByName(ReportColumn[i]).AsString = dr[row][i].ToString();
string str = dr[row][i].ToString();
}
}
Report.DetailGrid.Recordset.Post();
}
} } private void ReportFetchRecordDB()
{
for (int row = ; row < dgv.RowCount; row++)
{
Report.DetailGrid.Recordset.Append();
for (int i = ; i < ReportColumn.Length; i++)
{
if (dgv.Rows[row].Cells[i].Value != null)
{
Report.FieldByName(ReportColumn[i]).AsString = db.Rows[row][i].ToString();
}
}
Report.DetailGrid.Recordset.Post();
}
} /// <summary>
/// 去除按钮
/// </summary>
public void RemoveToolBar(GRPrintViewer pPrintViewer)
{
//GRPrintViewer pPrintViewer = new GRPrintViewer();
pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExport);
pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctMail);
pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctSaveDocument);
pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExportPDFBtn);
pPrintViewer.RemoveToolbarControl(GRToolControlType.grtctExportXLSBtn);
} /// <summary>
/// 打印预览
/// </summary>
public void printpreview()
{
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
Report.LoadFromFile(Application.StartupPath + "" + TemplatePath + "");
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
Report.ShowPreviewWnd += new _IGridppReportEvents_ShowPreviewWndEventHandler(RemoveToolBar); Report.PrintPreview(true);
} // GridReport .Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize); /// <summary>
/// 打印预览
/// </summary>
public void printpreviews()
{
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
Report.LoadFromVariant(tempReport);
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
Report.PrintPreview(true);
} /// <summary>
/// 打印预览
/// </summary>
public void printpreviews(bool flag)
{
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
Report.LoadFromVariant(tempReport);
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord); PrintDialog _Dialog = new PrintDialog();
Report.Printer.PrinterName = _Dialog.PrinterSettings.PrinterName;
Report.Print(false);
//Report.PrintPreview(true);
} /// <summary>
/// 设置列不可见
/// </summary>
/// <param name="list"></param>
private void ReportInitialize()
{
if (_list != null && _list.Count > )
{
foreach (string ColName in _list)
{
//IGRColumn iColName = Report.ColumnByName(ColName);
//if (iColName != null)
// iColName.Visible = false;
Report.ColumnByName(ColName).Visible = false;
}
}
} /// <summary>
/// 打印
/// </summary>
public void print()
{
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
Report.LoadFromFile(Application.StartupPath + "" + TemplatePath + "");
Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(Reportparameter);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
Report.Print(true);
}
}
}

最后就是打印信息了,这几步,自己根据需求放在合适位置,该封装方法封装

//下面为打印信息

            GridReport gr = new GridReport();

            string[] ReportParameter = new string[] {你的字段信息(对应顺序)};
gr.ReportParameter = ReportParameter;
string[] ReportPParameter = new string[];
for (int i = ; i < dt.Columns.Count; i++)
{
ReportPParameter[i] = string.Format("{0}", dt.Rows[][i]);
} gr.ReportPParameter = ReportPParameter;
gr.TemplatePath = "/名称.grf";
try
{
gr.printpreview();
}
catch
{
MessageBox.Show("打印出错了!");
return;
}
}

基本就完成了。。。但是差不多都是别人的代码,自己也就写了存储过程,修修补补。。。


ALTER PROCEDURE [dbo].[MY_BoarBaseInfoStoreProc]
@XXX varchar(100) //定义字段
as
begin
sql语句
end

 

C# 报表和打印等的更多相关文章

  1. 关于Web报表FineReport打印的开发应用案例

    报表打印是报表使用和开发过程中经常碰到的问题,这里汇总了关于Web报表开发打印功能的一些典型应用案例,以应用最广泛的FineReport为例. 案例一:java直接调用报表打印 当java后台定义定时 ...

  2. 吉特仓储管系统(开源WMS)--Web在线报表以及打印模板分享

    很早之前就想写这篇文章与大家分享一下自己在吉特仓储管理系统中开发打印和报表的功能,在GitHub(https://github.com/hechenqingyuan/gitwms)上公开下载的代码中很 ...

  3. php部分:网页中报表的打印,并用CSS样式控制打印的部分;

    网页中报表的打印,是通过调用window对象中的print()方法实现打印功能的: 调用浏览器本身的打印功能实现打印 <a href="#" onclick="wi ...

  4. ASP环境下轻松实现报表的打印 (转)

    作者:zcg2000 一.前言 ASP在电子商务上应用广泛,报表的处理又有一些麻烦.本文介绍了在ASP中利用 本人写的Report Server Page脚本解释器实现报表的显示.打印. 二.准备工作 ...

  5. .NET下Excel报表的打印

    说明:这是一个实验的小例子,在实际项目中使用时,一般Object[,] 对象的数据来源于数据库. 1. 实验环境 开发平台:Visual Studio 2010 测试模板:JBtest Excel:O ...

  6. RM报表的打印偏移

    自己摸索一下 RMReport1.SaveReportOptions.AutoLoadSaveSetting := True; RMReport1.SaveReportOptions.UseRegis ...

  7. 大数据量报表APPLET打印分页传输方案

     1 . 问题概述 当报表运算完成时,客户端经常需要调用润乾自带的runqianReport4Applet.jar来完成打印操作, 然而数据量比较大的时候,会导致无法加载完成,直至applet内存 ...

  8. 润乾V4报表批量打印

     背景说明 在应用中,经常遇到,批量打印的需求,批量打印,顾名思义,就是点击一次打印按钮,能打印多张报表. 下面,我们来介绍一下怎么样实现批量打印的 应用举例: Jsp代码 <% //rep ...

  9. 备忘录——基于rdlc报表实现打印产品标签

    目录 0. 背景说明 1. 条形码生成 2. 获取产品的小程序码 3. 报表设计器设计标签模版 3.1 为WinForm控件工具箱添加ReportViewer控件 3.2 为VS2019安装RDLC报 ...

随机推荐

  1. ORACLE常用数据库字段类型

    ORACLE常用数据库字段类型   常用的数据库字段类型如下:   字段类型 中文说明 限制条件 其它说明  CHAR 固定长度字符串 最大长度2000 bytes     VARCHAR2 可变长度 ...

  2. APNS消息推送实现

    转自:http://blog.csdn.net/biaobiaoqi/article/details/8058503 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1 ...

  3. docker 学习(一)什么是Docker

    项目中用到docker,就学习一下.第一篇是介绍. Sandboxie(沙箱):一个虚拟系统程序,允许你在沙盘环境中运行浏览器或其他程序,因此运行所产生的变化可以随后删除.它创造了一个类似沙盒的独立作 ...

  4. WPF语言切换,国际化

    winform语言切换在每个窗口下面有一个.resx结尾的资源文件,在上面添加新字符串就好了: WPF语言切换跟winform不一样的地方在于需要自己添加资源文件,并且这个资源文件可以写一个,也可以写 ...

  5. lightoj1009【DFS】

    思路: 连通快+二分图,每次+二分图大的元素个数. #include<bits/stdc++.h> using namespace std; typedef unsigned long l ...

  6. 剑指Offer的学习笔记(C#篇)-- 替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 一 . 自己的想法 老实说,貌 ...

  7. Windwos10环境下的Geany的安装与新手使用

    相信学习Python的小伙伴都会接触到Geany这个轻量级的python文本编辑器吧,下面就是我对于安装Geany的一些小经验分享. 1:安装geany很简单,进入geany官网download下载相 ...

  8. ios 自定义cell类中获取当前controller push

    有时候在自定义cell的过程中,当cell中又button的时候,把button的点击时间写在cell中的时候,需要获取到cell的父视图控制器然后push,可以自建一个类,命名为: GetCurre ...

  9. MyBaties源码解析

    觉得查看源代码确实是一个学习的一种方法 因为很多时候别人把最核心的代码给我们都封装好了 我们直接可以来拿使用 很多时候自己也会问 为什么通过这个方法就可以得到我觉得就是一颗好奇心吧 我算了算 就这三个 ...

  10. 两数之和LeetCode

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...