私は完全に動作するこのコードを持っています:
private void button3_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int i = 0;
int width = 0;
int height = 0;
int x = 0;
int y = 0;
StringFormat str = new StringFormat();
str.Alignment = StringAlignment.Near;
str.LineAlignment = StringAlignment.Center;
str.Trimming = StringTrimming.EllipsisCharacter;
Pen p = new Pen(Color.Black, 2.5f);
e.HasMorePages = true;
if (radioButton1.Checked)
{
while (i < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
height = 100;
width = 100;
e.HasMorePages = true;
return;
}
//Draws a rectangle with same width and height of first column of datagridview.
e.Graphics.DrawRectangle(Pens.Black, 500, 0, 125, dataGridView1.Rows[0].Height);
//Fills the above drawn rectangle with a light gray colour just to distinguish the header
e.Graphics.FillRectangle(Brushes.LightGray,newRectangle(500,0,125,dataGridView1.Rows[0].Height));
//Draws a text inside the above drawn rectangle whose value is same of the first column.
e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText, dataGridView1.Font,Brushes.Black, new RectangleF(500, 0,125, dataGridView1.Rows[0].Height), str);
height += dataGridView1.Rows[i].Height;
e.Graphics.DrawRectangle(Pens.Black, 400 + dataGridView1.Columns[3].Width, height, 125, dataGridView1.Rows[3].Height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), dataGridView1.Font,Brushes.Black, new RectangleF(400 + dataGridView1.Columns[0].Width,height, 400, dataGridView1.Rows[0].Height), str);
width += dataGridView1.Columns[0].Width;
i++;
}
}
radiobutton2 がチェックされ、それを出力するときのコードは次のとおりです。
if (radioButton2.Checked) {
while (i < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
height = 130;
width = 100;
e.HasMorePages = true;
return;
}
//Draws a text inside the above drawn rectangle whose value is same of the first column.
e.Graphics.DrawString(dataGridView1.Columns[3].HeaderText,dataGridView1.Font, Brushes.Black, new RectangleF(500, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
//Draws a rectangle with same width and height of first column of datagridview.
e.Graphics.DrawRectangle(Pens.Black, 600, 0, 200, dataGridView1.Rows[0].Height);
//Fills the above drawn rectangle with a light gray colour just to distinguish the header
e.Graphics.FillRectangle(Brushes.LightGray,newRectangle(600,0,200,dataGridView1.Rows[0].Height));
//Draws a text inside the above drawn rectangle whose value is same of the first column.
e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText,dataGridView1.Font,Brushes.Black, new RectangleF(600, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
height += dataGridView1.Rows[i].Height;
e.Graphics.DrawRectangle(Pens.Black, 100 + 300, height, 400, dataGridView1.Rows[4].Height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(),dataGridView1.Font,Brushes.Black,newRectangleF(500+dataGridView1.Columns[0].Width,height,500,dataGridView1.Rows[0]
.Height), str);
width += dataGridView1.Columns[0].Width;
i++;
}
}
がチェックされている場合radiobutton2
、印刷しますが、プレビューの生成が停止しません。何が問題なのかわからない。グーグルで調べてみましたが、答えが見つかりませんでした。誰か助けてくれませんか?