基于C#的WinForm打印程序附带源代码
程序员文章站
2022-06-24 19:34:43
...
【实例截图】
【核心代码】
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;
}
}
}
内容转载自网站: 阁楼网源 -最干净的资源网站
上一篇: Linux下的shell脚本