.Net常用技巧_打印DataGridView(转)
2020-12-13 03:37
标签:datagridview style blog color 使用 os 选择相应的列进行打印报表、只需实例化带一个DataGridView参数的构造函数就可以使用 .Net常用技巧_打印DataGridView(转),搜素材,soscw.com .Net常用技巧_打印DataGridView(转) 标签:datagridview style blog color 使用 os 原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811561.htmlclass PrintGridViewData
{
public PrintGridViewData()
{ }
private DataGridView DGridView;
public PrintGridViewData(DataGridView DGView)
{
DGridView = DGView;
}
private Form dialogForm = new Form();
private CheckedListBox CListBox = new CheckedListBox();
private Button Bton = new Button();
public void ControlAdd()
{
dialogForm.StartPosition = FormStartPosition.CenterParent;
dialogForm.Size = new Size(300, 330);
dialogForm.MinimizeBox = false;
dialogForm.MaximizeBox = false;
dialogForm.Text = "请选择打印的列";
dialogForm.Controls.Add(CListBox);
CListBox.Size = new Size(200, 300);
CListBox.Location = new Point(dialogForm.Location.X, dialogForm.Location.Y);
dialogForm.Controls.Add(Bton);
Bton.Text = "打印";
Bton.Size = new Size(75, 23);
Bton.Location = new Point(dialogForm.Location.X + 210, dialogForm.Location.Y + 180);
Bton.Click +=new EventHandler(Bton_Click);
for (int i = 0; i )
{
CListBox.Items.Add(DGridView.Columns[i].HeaderText);
}
dialogForm.Show();
}
private String[] ArrayStr;
private int ArrayCount=0;
private void Bton_Click(object sender, EventArgs e)
{
ArrayCount = CListBox.CheckedItems.Count;
ArrayStr = new String[ArrayCount];
for (int i = 0; i )
{
ArrayStr[i] = CListBox.CheckedItems[i].ToString();
}
PrintTable();
}
public void PrintTable()
{
dialogForm.Close();
PrintDocument PDocument = new PrintDocument();
PDocument.DocumentName = "打印Table";
PDocument.PrintPage +=new PrintPageEventHandler(PDocument_PrintPage);
PrintDialog PDialog = new PrintDialog();
PDialog.Document = PDocument;
PrintPreviewDialog PPreviewDialog = new PrintPreviewDialog();
PPreviewDialog.Document = PDocument;
PPreviewDialog.ClientSize = new System.Drawing.Size(1024, 768);
if (PDialog.ShowDialog() == DialogResult.OK)
{
PageSetupDialog PSetupDialog= new PageSetupDialog();
PSetupDialog.Document = PDocument;
PSetupDialog.Document.DefaultPageSettings.Landscape = true;//设置默认为横向打印
if (PSetupDialog.ShowDialog() == DialogResult.OK)
{
if (PPreviewDialog.ShowDialog() == DialogResult.OK)
{
PDocument.Print();
}
}
}
}
private int PrintRow = 0;
private void PDocument_PrintPage(object sender,PrintPageEventArgs e)
{
Graphics Gh = e.Graphics;
String Tablehead = "人事资料表"; //表头名称
Font headFont = new Font("宋体", 18, FontStyle.Bold); //表头字体
int headHeight = headFont.Height; //表头的占用的高度
Font FieldFont = new Font("宋体", 14, FontStyle.Bold); //表字段名的字体
int FieldHeight = FieldFont.Height; //表字段名占用的高度
Font DateFont = new Font("宋体", 12); //表头打印日期的字体
int DateHeight = DateFont.Height; //表头打印日期的字体占用的高度
Font SubjectFont = new Font("宋体", 13); //表主体文本的字体
int SubjectHeight = SubjectFont.Height; //表主体文本的字体占用的高度
int margin = 5; //字符串与单元格外框上下的间距
int ColumnsWidth = e.PageBounds.Width/ArrayCount; //列宽
Brush B = new SolidBrush(Color.Black);
Pen P = new Pen(Color.Black);
int x = e.PageBounds.X;
int y = e.PageBounds.Y;
int width = e.PageBounds.Width;
int height = e.PageBounds.Height-20;
//画表头名称
Gh.DrawString(Tablehead, headFont, B, x + width / 2 - ((int)(Gh.MeasureString(Tablehead, headFont)).Width / 2), 0);
//画跟在表头下面的打印日期(与表头名称间隔10px)
Gh.DrawString("打印日期:" + System.DateTime.Now.Date.ToString().Substring(0, 10), DateFont, B, x, y + headHeight + 10);
//画表字段名上的第一条横线
Gh.DrawLine(P, x, y + headHeight + 10 + DateHeight, x + width, y + headHeight + 10 + DateHeight);
//画表的字段名
int FieldIndex = 0;
for (int i = 0; i )
{
//当字符串数组ArrayStr包含指定的字符串时返回true
bool trueORfalse = ((IList