基于C#的WinForm打印程序附带源代码

【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinFormsPrint
{
public partial class FormPrint : Form
{
string no;
public FormPrint(string no)
{
this.no = no;
InitializeComponent();
}

    private void FormPrint_Load(object sender, EventArgs e)
    {
        this.reportViewer1.LocalReport.DataSources.Clear();
        this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", GetList()));

        //显示报表
        this.reportViewer1.RefreshReport();
        
    }
    
    /// <summary>
    /// 获取打印的数据源
    /// </summary>
    /// <returns></returns>
    private List<Student> GetList()
    {

        string selectSql = "select * from Student ";
        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="   System.Windows.Forms.Application.StartupPath.ToString().Replace("bin\\Debug", "DB\\Student.accdb");
        if (!string.IsNullOrEmpty(no))
        {
            selectSql  = "where StudentNo='"   no   "'";
        }


        OleDbConnection con = new OleDbConnection(connStr);
        con.Open();
        OleDbDataAdapter da = new OleDbDataAdapter(selectSql, con);
        DataTable table = new DataTable();
        da.Fill(table);
        con.Close();

        List<Student> list = new List<Student>();
        Student stu = null;
        foreach (DataRow row in table.Rows)
        {
            stu = new Student();
            if (row.Table.Columns.Contains("StudentNo") && row["StudentNo"] != null && row["StudentNo"].ToString() != "")
            {
                stu.StudentNo = row["StudentNo"].ToString();
            }
            if (row.Table.Columns.Contains("StudentName") && row["StudentName"] != null && row["StudentName"].ToString() != "")
            {
                stu.StudentName = row["StudentName"].ToString();
            }
            if (row.Table.Columns.Contains("StudentAge") && row["StudentAge"] != null && row["StudentAge"].ToString() != "")
            {
                stu.StudentAge = Convert.ToInt32(row["StudentAge"]);
            }
            if (row.Table.Columns.Contains("StudentContent") && row["StudentContent"] != null && row["StudentContent"].ToString() != "")
            {
                stu.StudentContent = row["StudentContent"].ToString();
            }
            list.Add(stu);
        }
        return list;
    }
}

}
内容转载自网站: 阁楼网源 -最干净的资源网站在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值