C#实现水晶报表绑定数据并实现打印

步骤:

1.新建一个数据集,然后将数据库里面的表托放到DataSet2.xsd里面

      

  2.新建一个水晶报表,根据向导完成水晶报表的制作,效果图如下:

   

  3.新建一个WINDOWS窗体,并在窗体里面放置一个CrystalReportViewer控件,单击右上角的小三角行,然后选择需要显示的报表,并设置报表的路径,

  效果图如下:

  

  4.接下来是后台代码的绑定了:

 

01//绑定开始
02String sql=""
03sql = "select * from 表名"
04SqlConnection con=new Sqlconnection("数据连接字符串");
05SqlDataApter da=new SqlDataApter(sql,con);
06DataSet ds=new DataSet();
07CrystalReport1  cry=new CrystalReport1 ();
08con.Open()
09da.Fill(ds, "dgv")
10cry.SetDataSource(ds.Tables("dgv"))
11CrystalReportViewer1.ReportSource = cry
12//绑定结束
13如果要实现打印功能,还需修改一下,以下是打印的功能:(这个打印功能不是CrystalReportViewer1里面自带的):
 
 
01<PRE class=brush:csharp>String sql="";
02sql = "select * from Employee";
03SqlClient.SqlConnection con=null;
04SqlClient.SqlDataAdapter da=null;
05DataSet ds=new DataSet();
06con = New SqlClient.SqlConnection("Data Source=PC-201005072138;Initial Catalog=Company;Persist Security Info=True;User ID=sa;Password=ddddd");
07con.Open();
08da = New SqlClient.SqlDataAdapter(sql, con)
09da.Fill(ds, "main")
10//打印功能
11PrintDocument  printDt = New PrintDocument();//打印文档对象
12ReportDocument rd= New ReportDocument();//报表对象
13rd.Load(Application.StartupPath + "\CrystalReport1.rpt");
14//MessageBox.Show(Application.StartupPath + "\CrystalReport1.rpt");
15rd.SetDatabaseLogon("sa", "ddddd");
16rd.SetDataSource(ds);
17String strDefaultPrinter= printDt.PrinterSettings.PrinterName;
18rd.PrintOptions.PrinterName = strDefaultPrinter;
19rd.PrintToPrinter(1, True, 0, 0);
20//打印结束 </PRE>
21<PRE class=brush:csharp>以上代码是个人在做的时候用的,如有什么地方错误的话,欢迎留言...

转载于:https://www.cnblogs.com/yongheng178/archive/2010/07/15/1778268.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
不使用水晶报表实现列表式的打印。 刚学C# 1个月时写的,如果污染了视听,请见谅。 struct PageProperty { public int PageLeft; public int PageTop; public int PageBottom; public int PageWidth; public int PageHeight; public int RowSpace; public int ColSpace; public int AllVisibleColSpace; public int AllColWidth; public int AllVisibleColWidth; public int AllRowHeight; public int TitleH; public int SubTitleH; public int FooterH; public int[] ColWidth; public int VisibleColWidth; public int PageTextHeight; public int PageTextWidth; } struct TRect { public int Left; public int Top; public int Right; public int Bottom; } class PanelImage : Panel { protected Panel panelShadow = null; public PictureBox ImgData = null; public PanelImage() { this.BackColor = Color.Gray; this.DoubleBuffered = true; ImgData = new PictureBox(); ImgData.Parent = this; ImgData.BackColor = Color.White; panelShadow = new Panel(); panelShadow.Parent = this; panelShadow.BackColor = Color.Black; ResizeControl(); } void ResizeControl() { panelShadow.Left = 8; panelShadow.Top = 8; panelShadow.Width = this.Width - 12; panelShadow.Height = this.Height - 12; ImgData.Left = 4; ImgData.Top = 4; ImgData.Width = this.Width - 12; ImgData.Height = this.Height - 12; } protected override void OnResize(EventArgs eventargs) { ResizeControl(); base.OnResize(eventargs); } protected override void Dispose(bool disposing) { panelShadow.Dispose(); ImgData.Dispose(); base.Dispose(disposing); } } public partial class PrintGrid { [DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", CallingConvention = CallingConvention.StdCall)] public static extern int GetDeviceCaps(int Handle, int nIndex); private float _leftMargin = 0; private float _topMargin = 0; private float _rightMargin = 0; private float _bottomMargin = 0; const short HORZSIZE = 4; const short VERTSIZE = 6; const short HORZRES = 8; const short VERTRES = 10; const short PHYSICALOFFSETX = 112; const short PHYSICALOFFSETY = 113; int ixLogPixPerInch; int iyLogPixPerInch; int xLogPixPerInch; int yLogPixPerInch; int FPageIndex; int FPageCount; string FLeftPageFooter; string FRightPageFooter; string FTitle; Font FFont; Font FTitleFont; Font FTextFont; Font FFooterFont; Font FSubTitleFont; int FSubTitleOff; int FLeftPageFooterOff; int FPrintCount; string FSubTitle; bool FHLine; bool FVLine; int FPageTop; int FPageLeft; int FPageRight; int FPageBottom; int FToPage; int FFromPage; bool FPrintPageCount; string FRightSubTitle; bool FExistPrinter; bool FCenterShow; Color FHeaderColor; bool FUseHeaderColor; private int iDX; //水平缩放比例 private int iDY; //垂直缩放比例 private int ColCount; //总列数 private int RowCount; //总行数 private int[] ColWidth; //记录每一列的宽度 private int AllColWidth; //所有列的宽度 private int AllVisibleColWidth; //所有可视列的宽度 private int RowSpace; //行间距 private int ColSpace; //所有可视列间距 private int AllVisibleColSpace; //所有行的间距 private int AllColSpace; //所有列的间距 private int AllRowSpace; //所有行的间距 private int RowsOfPage; //每页打的行数 private int PageWidth; //页的宽度 private int PageHeight; //页的高度 private int PageTextHeight; //页正文高度 private int PageTextWidth; //页正文宽度 private int TextTop; //正文上边距 private int TextLeft; //正文左边距 private int TitleH; //标题高度 private int SubTitleH; //子标题高度 private int FooterH; //页脚高度 private int FooterTop; //页脚顶部 private int TextH; //正文高度 private int VisibleColWidth; private int DX; private int DY; private string StrTest; private ListView ListViewData = null; //数据 private PrintDocument PD = null;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值