0% found this document useful (0 votes)
585 views3 pages

Print Datagridview C# Code

The document appears to be code for printing the contents of a data grid view control to a printer. It includes code for drawing column headers, cell values, and handling pagination across multiple pages when printing a data grid with many rows. Variables are used to track the drawing position and determine when a new page is needed.

Uploaded by

ciaossu12345
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
585 views3 pages

Print Datagridview C# Code

The document appears to be code for printing the contents of a data grid view control to a printer. It includes code for drawing column headers, cell values, and handling pagination across multiple pages when printing a data grid with many rows. Variables are used to track the drawing position and determine when a new page is needed.

Uploaded by

ciaossu12345
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using using using using using using using

System; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];

namespace PrintGrid { public partial class Form1 : Form { Bitmap bm; int i = 0; string[] names=new string[] {"Amey","vijay","Ranjit","Bharat","saba"}; public Form1() { InitializeComponent(); FillDataGridView(); } private void FillDataGridView() { DataTable dt = new DataTable(); DataRow dr = null; [Link]("Id"); [Link]("Name"); for (int i = 0; i < 100 ; i++) { dr = [Link](); dr["Id"] = i + 1; dr["Name"] = names[i % 5]; [Link](dr); } [Link] = dt; } private void button1_Click(object sender, EventArgs e) { [Link](); //[Link](); //if ([Link]() == [Link]) //{ // [Link] = printDocument1; //} } private void printDocument1_BeginPrint(object sender, [Link] [Link] e) { } private void printDocument1_PrintPage(object sender, [Link] [Link] e) { int width = 0;

int height = 0; //int x = 0; //int y = 0; //int rowheight = 0; //int columnwidth = 0; StringFormat str=new StringFormat(); [Link]=[Link]; [Link]=[Link]; [Link]=[Link]; Pen p=new Pen([Link],2.5f); //[Link](p,[Link]); //[Link]=printDocument1; //[Link]("Hi this a test print", new Font([Link] FontName,10.5f), [Link], new PointF(250.0f, 250.0f)); //[Link]((Image)bm,new Point(10,10)); #region Draw Column 1 [Link]([Link], new Rectangle(100, 100, [Link][0].Width, [Link][0].Height)); [Link]([Link], 100, 100, [Link] [0].Width, [Link][0].Height); [Link]([Link][0].HeaderText, dataGridV [Link], [Link], new RectangleF(100, 100, [Link][0] .Width, [Link][0].Height), str); #endregion #region Draw column 2 [Link]([Link], new Rectangle(100 + data [Link][0].Width, 100, [Link][0].Width, [Link][0].Height)); [Link]([Link], 100 + [Link][0]. Width, 100, [Link][0].Width, [Link][0].Height); [Link]([Link][1].HeaderText, dataGridV [Link], [Link], new RectangleF(100 + [Link][0].Wid th, 100, [Link][0].Width, [Link][0].Height ), str); width = 100 + [Link][0].Width; height = 100; //variable i is declared at class level to preserve the value of i i f [Link] is true while (i < [Link])

{ if (height > [Link]) { height = 100; width = 100; [Link] = true; return; } height += [Link][i].Height; [Link]([Link], 100, height, dataGridView1. Columns[0].Width, [Link][0].Height); [Link]([Link][i].Cells[0].FormattedVa [Link](), [Link], [Link], new RectangleF(100, heigh t, [Link][0].Width, [Link][0].He ight), str); [Link]([Link], 100 + [Link] [0].Width, height, [Link][0].Width, [Link][0 ].Height); [Link]([Link][i].Cells[1].[Link] ing(), [Link], [Link], new RectangleF(100 + data [Link][0].Width, height, [Link][0].Width, [Link] ws[0].Height), str); width += [Link][0].Width; i++; } #endregion } private void button2_Click(object sender, EventArgs e) { bm = new Bitmap([Link], dataGridView1.C [Link]); [Link](bm, [Link]); [Link](@"C:\datagrid", [Link]); bm = null; bm = new Bitmap([Link], [Link] [Link]); [Link](bm, [Link]); } } }

You might also like