Печать документа из DataGridView

Небольшой пример, как можно выполнить печать с данными из DataGridView на языке C#. Есть маленькие недостатки, печатает только одну страницу. Код взят из интернета.

Обработчик события печати:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            int x = 0;
            int y = 20;
            int cell_height = 0;
 
            int colCount = dataGridView1.ColumnCount;
            int rowCount = dataGridView1.RowCount - 1;
 
            Font font = new Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
 
            int[] widthC = new int[colCount];
 
            int current_col = 0;
            int current_row = 0;
 
            while (current_col < colCount)             {                 if (g.MeasureString(dataGridView1.Columns[current_col].HeaderText.ToString(), font).Width > widthC[current_col])
                    {
                        widthC[current_col] = (int)g.MeasureString(dataGridView1.Columns[current_col].HeaderText.ToString(), font).Width;
                    }
                current_col++;
            }
 
            while (current_row < rowCount)
            {
                while (current_col < colCount)                 {                     if (g.MeasureString(dataGridView1[current_col, current_row].Value.ToString(), font).Width > widthC[current_col])
                    {
                        widthC[current_col] = (int)g.MeasureString(dataGridView1[current_col, current_row].Value.ToString(), font).Width;
                    }
                    current_col++;
                }
                current_col = 0;
                current_row++;
            }
 
            current_col = 0;
            current_row = 0;
 
            string value = "";
 
            int width = widthC[current_col];
            int height = dataGridView1[current_col, current_row].Size.Height;
 
            Rectangle cell_border;
            SolidBrush brush = new SolidBrush(Color.Black);
 
 
            while (current_col < colCount)
            {
                width = widthC[current_col];
                cell_height = dataGridView1[current_col, current_row].Size.Height;
                cell_border = new Rectangle(x, y, width, height);
                value = dataGridView1.Columns[current_col].HeaderText.ToString();
                g.DrawRectangle(new Pen(Color.Black), cell_border);
                g.DrawString(value, font, brush, x, y);
                x += widthC[current_col];
                current_col++;
            }
            while (current_row < rowCount)
            {
                while (current_col < colCount)
                {
                    width = widthC[current_col];
                    cell_height = dataGridView1[current_col, current_row].Size.Height;
                    cell_border = new Rectangle(x, y, width, height);
                    value = dataGridView1[current_col, current_row].Value.ToString();
                    g.DrawRectangle(new Pen(Color.Black), cell_border);
                    g.DrawString(value, font, brush, x, y);
                    x += widthC[current_col];
                    current_col++;
                }
                current_col = 0;
                current_row++;
                x = 0;
                y += cell_height;
            }
        }

Пример вызова:

1
2
3
4
5
6
7
8
9
private void button2_Click(object sender, EventArgs e)
{
PrintDocument Document = new PrintDocument();
Document.PrintPage += new PrintPageEventHandler(print_dgv_PrintPage);
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = Document;
 
dlg.ShowDialog();
}

Оставить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *