cscの作法 その177

# 概要
cscの作法、調べてみた。
datagridviewを印刷してみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/a868b63d-59bf-f0a2-6292-81f343aa741b.png)

# サンプルコード

```
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;

namespace app
{
	class form1: Form {
		private Button button1;
		private DataGridView gd0;
		private DataTable dt;
		private OleDbConnection con;
		private DataGridViewPrinter myPrintDataGrid;
		form1() {
			Text = "DataGridView";
			ClientSize = new Size(700, 400);
			this.button1 = new Button();
			this.button1.Location = new System.Drawing.Point(540, 60);
			this.button1.TabIndex = 0;
			this.button1.Text = "test";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.Controls.Add(this.button1);
			this.gd0 = new DataGridView();
			this.gd0.Location = new System.Drawing.Point(30, 30);
			this.gd0.Size = new Size(500, 370);
			this.Controls.Add(this.gd0);
			con = new OleDbConnection();
			con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
			con.Open();
			OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM sisyutu;", con);
			dt = new DataTable();
			oda.Fill(dt);
			gd0.DataSource = dt;
			con.Close();
		}
		private void button1_Click(object sender, System.EventArgs e) {
			PrintPreviewDialog curPreviewDialog = new PrintPreviewDialog();
			PrintDialog curPrintDialog = new PrintDialog();
			bool CenterOnPage = true;
			bool WithTitle = true;
			bool WithPaging = true;
			Font fontPrintTitle = new Font("Arial Narrow", 14, FontStyle.Bold);
			Font fontPrintFooter = new Font("Times New Roman", 10);
			curPrintDialog.AllowCurrentPage = true;
			curPrintDialog.AllowPrintToFile = false;
			curPrintDialog.AllowSelection = true;
			curPrintDialog.AllowSomePages = true;
			curPrintDialog.PrintToFile = false;
			curPrintDialog.ShowHelp = false;
			curPrintDialog.ShowNetwork = true;
			curPrintDialog.UseEXDialog = true;
			curPrintDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
			if (curPrintDialog.ShowDialog() == DialogResult.OK)
			{
				String printTitle = this.Text;
				PrintDocument myPrintDoc = new PrintDocument();
				myPrintDoc.DocumentName = this.Text;
				myPrintDoc.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
				myPrintDoc.DefaultPageSettings.Landscape = true;
				myPrintDataGrid = new DataGridViewPrinter(gd0, myPrintDoc, CenterOnPage, WithTitle, printTitle, fontPrintTitle, Color.DarkBlue, WithPaging);
				myPrintDoc.PrinterSettings = curPrintDialog.PrinterSettings;
				myPrintDoc.DefaultPageSettings = curPrintDialog.PrinterSettings.DefaultPageSettings;
				myPrintDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
				curPreviewDialog.Document = myPrintDoc;
				curPreviewDialog.ShowDialog();
			}
			Console.WriteLine("ok");
		}
		private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
			bool more = myPrintDataGrid.DrawDataGridView(e.Graphics);
			if (more)
				e.HasMorePages = true;
		}
		[STAThread]
		public static void Main() {
			Application.Run(new form1());
		}
	}
}





```

以上。