C#打印预览DataGridView数据

 我来答
钱多多0970
推荐于2016-06-26 · TA获得超过350个赞
知道小有建树答主
回答量:1148
采纳率:0%
帮助的人:404万
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

using System.Drawing.Printing;

namespace StudentManager
{
public partial class PrintGridView : Form
{
string sSqlConnection = @"Data Source=local;Initial Catalog=StudentManager;User ID=sa;Password=123456";

//打印文档
PrintDocument pdDocument = new PrintDocument();

//打印格式设置页面
PageSetupDialog dlgPageSetup = new PageSetupDialog();

//打印页面
PrintDialog dlgPrint = new PrintDialog();
private Button btnPrint;
private Button btnPrintView;
private DataGridView dgvData;
private PrintPreviewDialog printPreviewDialog1;
private PrintDocument printDocument1;

//实例化打印预览
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();

public PrintGridView()
{
InitializeComponent();
btnPrint.Click += new EventHandler(btnPrint_Click);
btnPrintView.Click += new EventHandler(btnPrintView_Click);
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);

//页面设置的打印文档设置为需要打印的文档
dlgPageSetup.Document = pdDocument;

//打印界面的打印文档设置为被打印文档
dlgPrint.Document = pdDocument;

//打印预览的打印文档设置为被打印文档
dlgPrintPreview.Document = pdDocument;

GetData();
}

///
/// 打印预览
///
///
///
void btnPrintView_Click(object sender, EventArgs e)
{
//显示打印预览界面
dlgPrintPreview.ShowDialog();
}

private void GetData()
{
SqlConnection scConnect = new SqlConnection(sSqlConnection);
scConnect.Open();

SqlCommand scCommand = scConnect.CreateCommand();
scCommand.CommandText = "select customerID,companyName from customers";

SqlDataAdapter sdaAdapter = new SqlDataAdapter(scCommand);
DataTable dt = new DataTable();
sdaAdapter.Fill(dt);

dgvData.DataSource = dt;

scCommand.Dispose();
scConnect.Close();
sdaAdapter.Dispose();

}

///
/// DataGridView转换为二维数组
///
///
///
///
private string[,] ToStringArray(DataGridView dataGridView, bool includeColumnText)
{

string[,] arrReturn = null;
int rowsCount = dataGridView.Rows.Count;
int colsCount = dataGridView.Columns.Count;
if (rowsCount > 0)
{
//最后一行是供输入的行时,不用读数据。
if (dataGridView.Rows[rowsCount - 1].IsNewRow)
{
rowsCount--;
}
}
int i = 0;
//包括列标题
if (includeColumnText)
{
rowsCount++;
arrReturn = new string[rowsCount, colsCount];
for (i = 0; i < colsCount; i++)
{
arrReturn[0, i] = dataGridView.Columns[i].HeaderText;
}
i = 1;
}
else
{
arrReturn = new string[rowsCount, colsCount];
}
//读取单元格数据
int rowIndex = 0;
for (; i < rowsCount; i++, rowIndex++)
{
for (int j = 0; j < colsCount; j++)
{
arrReturn[i, j] = dataGridView.Rows[rowIndex].Cells[j].Value.ToString();
}
}
return arrReturn;
}

///
/// 打印
///
///
///
void btnPrint_Click(object sender, EventArgs e)
{
pdDocument.Print();
}

///
/// printDocument的PrintPage事件
///
///
///
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int iX = 10;
int iY = 10;
printdatagrid.print(dgvData, true, e, ref iX, ref iY);
}

///
/// EndPrint事件释放BeginPrint方法中占用的资源
///
///
///
void pdDocument_EndPrint(object sender, PrintEventArgs e)
{

}

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PrintGridView));
this.btnPrint = new System.Windows.Forms.Button();
this.btnPrintView = new System.Windows.Forms.Button();
this.dgvData = new System.Windows.Forms.DataGridView();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
((System.ComponentModel.ISupportInitialize)(this.dgvData)).BeginInit();
this.SuspendLayout();
//
// btnPrint
//
this.btnPrint.Location = new System.Drawing.Point(482, 73);
this.btnPrint.Name = "btnPrint";
this.btnPrint.Size = new System.Drawing.Size(75, 23);
this.btnPrint.TabIndex = 0;
this.btnPrint.Text = "button1";
this.btnPrint.UseVisualStyleBackColor = true;
//
// btnPrintView
//
this.btnPrintView.Location = new System.Drawing.Point(482, 168);
this.btnPrintView.Name = "btnPrintView";
this.btnPrintView.Size = new System.Drawing.Size(75, 23);
this.btnPrintView.TabIndex = 1;
this.btnPrintView.Text = "button2";
this.btnPrintView.UseVisualStyleBackColor = true;
//
// dgvData
//
this.dgvData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvData.Location = new System.Drawing.Point(-3, 12);
this.dgvData.Name = "dgvData";
this.dgvData.RowTemplate.Height = 23;
this.dgvData.Size = new System.Drawing.Size(408, 440);
this.dgvData.TabIndex = 2;
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// PrintGridView
//
this.ClientSize = new System.Drawing.Size(787, 527);
this.Controls.Add(this.dgvData);
this.Controls.Add(this.btnPrintView);
this.Controls.Add(this.btnPrint);
this.Name = "PrintGridView";
((System.ComponentModel.ISupportInitialize)(this.dgvData)).EndInit();
this.ResumeLayout(false);

}

}
/////
///// 实现DataGridView的打印
/////
//public class PrintDataGridView
//{
// private static List CellPrintList = new List();

// private static int printRowCount = 0;

// private static bool IsPrint = true;
// private static bool IsRole = true;
// private static int PoXTmp = 0;
// private static int PoYTmp = 0;
// private static int WidthTmp = 0;
// private static int HeightTmp = 0;
// private static int RowIndex = 0;

// ///
// /// 打印DataGridView控件
// ///
// /// DataGridView控件
// /// 是否包括列标题
// /// 为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。
// /// 起始X坐标
// /// 起始Y坐标
// public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs eValue, ref int PoX, ref int PoY)
// {
// try
// {
// if (PrintDataGridView.IsPrint)
// {
// PrintDataGridView.printRowCount = 0;
// PrintDataGridView.IsPrint = false;
// PrintDataGridView.DataGridViewCellVsList(dataGridView, includeColumnText);
// if (0 == PrintDataGridView.CellPrintList.Count)
// return;
// if (PoX > eValue.MarginBounds.Left)
// PrintDataGridView.IsRole = true;
// else
// PrintDataGridView.IsRole = false;
// PrintDataGridView.PoXTmp = PoX;
// PrintDataGridView.PoYTmp = PoY;
// PrintDataGridView.RowIndex = 0;
// WidthTmp = 0;
// HeightTmp = 0;
// }
// if (0 != PrintDataGridView.printRowCount)
// {
// if (IsRole)
// {
// PoX = PoXTmp = eValue.MarginBounds.Left;
// PoY = PoYTmp = eValue.MarginBounds.Top;
// }
// else
// {
// PoX = PoXTmp;
// PoY = PoYTmp;
// }
// }
// while (PrintDataGridView.printRowCount < PrintDataGridView.CellPrintList.Count)
// {
// DataGridViewCellPrint CellPrint = CellPrintList[PrintDataGridView.printRowCount];
// if (RowIndex == CellPrint.RowIndex)
// PoX = PoX + WidthTmp;
// else
// {
// PoX = PoXTmp;
// PoY = PoY + HeightTmp;
// if (PoY + HeightTmp > eValue.MarginBounds.Bottom)
// {
// HeightTmp = 0;
// eValue.HasMorePages = true;
// return;
// }
// }
// using (SolidBrush solidBrush = new SolidBrush(CellPrint.BackColor))
// {
// RectangleF rectF1 = new RectangleF(PoX, PoY, CellPrint.Width, CellPrint.Height);
// eValue.Graphics.FillRectangle(solidBrush, rectF1);
// using (Pen pen = new Pen(Color.Black, 1))
// eValue.Graphics.DrawRectangle(pen, Rectangle.Round(rectF1));
// solidBrush.Color = CellPrint.ForeColor;
// eValue.Graphics.DrawString(CellPrint.FormattedValue, CellPrint.Font, solidBrush, new Point(PoX + 2, PoY + 3));
// }
// WidthTmp = CellPrint.Width;
// HeightTmp = CellPrint.Height;
// RowIndex = CellPrint.RowIndex;
// PrintDataGridView.printRowCount++;
// }
// PoY = PoY + HeightTmp;
// eValue.HasMorePages = false;
// PrintDataGridView.IsPrint = true;
// }
// catch
// {
// eValue.HasMorePages = false;
// PrintDataGridView.IsPrint = true;
// throw;
// }

// }

// ///
// /// 将DataGridView控件内容转变到 CellPrintList
// ///
// /// DataGridView控件
// /// 是否包括列标题
// private static void DataGridViewCellVsList(DataGridView dataGridView, bool includeColumnText)
// {
// CellPrintList.Clear();
// try
// {
// int rowsCount = dataGridView.Rows.Count;
// int colsCount = dataGridView.Columns.Count;

// //最后一行是供输入的行时,不用读数据。
// if (dataGridView.Rows[rowsCount - 1].IsNewRow)
// rowsCount--;
// //包括列标题
// if (includeColumnText)
// {
// for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)
// {
// if (dataGridView.Columns[columnsIndex].Visible)
// {
// DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();
// CellPrint.FormattedValue = dataGridView.Columns[columnsIndex].HeaderText;
// CellPrint.RowIndex = 0;
// CellPrint.ColumnIndex = columnsIndex;
// CellPrint.Font = dataGridView.Columns[columnsIndex].HeaderCell.Style.Font;
// CellPrint.BackColor = dataGridView.ColumnHeadersDefaultCellStyle.BackColor;
// CellPrint.ForeColor = dataGridView.ColumnHeadersDefaultCellStyle.ForeColor;
// CellPrint.Width = dataGridView.Columns[columnsIndex].Width;
// CellPrint.Height = dataGridView.ColumnHeadersHeight;
// CellPrintList.Add(CellPrint);
// }
// }
// }
// //读取单元格数据
// for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
// {
// for (int columnsIndex = 0; columnsIndex < colsCount; columnsIndex++)
// {
// if (dataGridView.Columns[columnsIndex].Visible)
// {
// DataGridViewCellPrint CellPrint = new DataGridViewCellPrint();
// CellPrint.FormattedValue = dataGridView.Rows[rowIndex].Cells[columnsIndex].FormattedValue.ToString();
// if (includeColumnText)
// CellPrint.RowIndex = rowIndex + 1;//假如包括列标题则从行号1开始
// else
// CellPrint.RowIndex = rowIndex;
// CellPrint.ColumnIndex = columnsIndex;
// CellPrint.Font = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.Font;
// System.Drawing.Color TmpColor = System.Drawing.Color.Empty;
// if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)
// TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;
// else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor)
// TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor;
// else
// TmpColor = dataGridView.DefaultCellStyle.BackColor;
// CellPrint.BackColor = TmpColor;
// TmpColor = System.Drawing.Color.Empty;
// if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor)
// TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor;
// else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor)
// TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor;
// else
// TmpColor = dataGridView.DefaultCellStyle.ForeColor;
// CellPrint.ForeColor = TmpColor;
// CellPrint.Width = dataGridView.Columns[columnsIndex].Width;
// CellPrint.Height = dataGridView.Rows[rowIndex].Height;
// CellPrintList.Add(CellPrint);
// }
// }
// }
// }
// catch { throw; }
// }

// private class DataGridViewCellPrint
// {
// private string _FormattedValue = "";
// private int _RowIndex = -1;
// private int _ColumnIndex = -1;
// private System.Drawing.Color _ForeColor = System.Drawing.Color.Black;
// private System.Drawing.Color _BackColor = System.Drawing.Color.White;
// private int _Width = 100;
// private int _Height = 23;
// private System.Drawing.Font _Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
// ///
// /// 获取或设置单元格的字体。
// ///
// public System.Drawing.Font Font
// {
// set { if (null != value) _Font = value; }
// get { return _Font; }
// }
// ///
// /// 获取为显示进行格式化的单元格的值。
// ///
// public string FormattedValue
// {
// set { _FormattedValue = value; }
// get { return _FormattedValue; }
// }
// ///
// /// 获取或设置列的当前宽度 (以像素为单位)。默认值为 100。
// ///
// public int Width
// {
// set { _Width = value; }
// get { return _Width; }
// }
// ///
// /// 获取或设置列标题行的高度(以像素为单位)。默认值为 23。
// ///
// public int Height
// {
// set { _Height = value; }
// get { return _Height; }
// }
// ///
// /// 获取或设置行号。
// ///
// public int RowIndex
// {
// set { _RowIndex = value; }
// get { return _RowIndex; }
// }
// ///
// /// 获取或设置列号。
// ///
// public int ColumnIndex
// {
// set { _ColumnIndex = value; }
// get { return _ColumnIndex; }
// }
// ///
// /// 获取或设置前景色。
// ///
// public System.Drawing.Color ForeColor
// {
// set { _ForeColor = value; }
// get { return _ForeColor; }
// }
// ///
// /// 获取或设置背景色。
// ///
// public System.Drawing.Color BackColor
// {
// set { _BackColor = value; }
// get { return _BackColor; }
// }

// }

//}

}
蒙蒙lm
2010-06-24 · 超过10用户采纳过TA的回答
知道答主
回答量:67
采纳率:0%
帮助的人:34.8万
展开全部
有直接用打印控件打印,还可以用水晶报表打印,还可以导出Excel打印
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
跃月越悦
2010-06-24 · TA获得超过132个赞
知道答主
回答量:27
采纳率:0%
帮助的人:9.5万
展开全部
jkdtbhtyeb
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
liyangfd
2010-06-24 · TA获得超过181个赞
知道小有建树答主
回答量:232
采纳率:50%
帮助的人:92.2万
展开全部
不会
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式