C# datagridview winforms的打印问题
大家好,我已经编写了一个代码来打印datagridview行,它工作正常,但我在第二页中没有看到数据。有人能帮我吗,这是我尝试过的代码。我有90行,我尝试了其他一些文章,但没有运气C# datagridview winforms的打印问题,c#,winforms,datagridview,C#,Winforms,Datagridview,大家好,我已经编写了一个代码来打印datagridview行,它工作正常,但我在第二页中没有看到数据。有人能帮我吗,这是我尝试过的代码。我有90行,我尝试了其他一些文章,但没有运气 private void BindGrid() { string constring = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"; using (SqlConnection con
private void BindGrid()
{
string constring = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Set AutoGenerateColumns False
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count
dataGridView1.ColumnCount = 3;
//Add Columns
dataGridView1.Columns[0].Name = "CustomerId";
dataGridView1.Columns[0].HeaderText = "Customer Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerID";
dataGridView1.Columns[1].HeaderText = "Contact Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "ContactName";
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";
dataGridView1.DataSource = dt;
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
BindGrid();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int height, width = 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);
System.Drawing.Font fntString = new Font("Times New Roman", 10, FontStyle.Bold);
System.Drawing.Font fnthead = new Font("Arial", 20, FontStyle.Bold);
height = 100;
while (s < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
height = 100;
width = 100;
e.HasMorePages = true;
return;
}
height += dataGridView1.Rows[s].Height;
if (s % 2 == 0)
{
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[0].Value.ToString(), fntString, Brushes.Black, 70, (s * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[1].Value.ToString(), fntString, Brushes.Black, 100, (s * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[2].Value.ToString(), fntString, Brushes.Black, 70, (s * 89) + 40);
}
else
{ // Right Column
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[0].Value.ToString(), fntString, Brushes.Black, 370, ((s - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[1].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[s].Cells[2].Value.ToString(), fntString, Brushes.Black, 370, ((s - 1) * 89) + 40);
}
s++;
}
}
private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void BindGrid()
{
字符串结构=@“数据源=;初始目录=北风;集成安全=真”;
使用(SqlConnection con=新的SqlConnection(consting))
{
使用(SqlCommand cmd=newsqlcommand(“从客户中选择*,con))
{
cmd.CommandType=CommandType.Text;
使用(SqlDataAdapter sda=newsqldataadapter(cmd))
{
使用(DataTable dt=newdatatable())
{
sda.填充(dt);
//将AutoGenerateColumns设置为False
dataGridView1.AutoGenerateColumns=false;
//设置列数
dataGridView1.ColumnCount=3;
//添加列
dataGridView1.Columns[0]。Name=“CustomerId”;
dataGridView1.Columns[0]。HeaderText=“客户Id”;
dataGridView1.Columns[0]。DataPropertyName=“CustomerID”;
dataGridView1.Columns[1]。HeaderText=“联系人姓名”;
dataGridView1.Columns[1].Name=“Name”;
dataGridView1.Columns[1]。DataPropertyName=“ContactName”;
dataGridView1.Columns[2].Name=“Country”;
dataGridView1.Columns[2]。HeaderText=“Country”;
dataGridView1.Columns[2].DataPropertyName=“Country”;
dataGridView1.DataSource=dt;
}
}
}
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
BindGrid();
}
私有void printDocument1_PrintPage(对象发送方,PrintPageEventArgs e)
{
整数高度,宽度=0;
StringFormat str=新的StringFormat();
str.Alignment=StringAlignment.Near;
str.LineAlignment=StringAlignment.Center;
str.Trimming=StringTrimming.EllipsisCharacter;
钢笔p=新钢笔(颜色为黑色,2.5f);
System.Drawing.Font fntString=新字体(“Times new Roman”,10,FontStyle.Bold);
System.Drawing.Font fnthead=新字体(“Arial”,20,FontStyle.Bold);
高度=100;
而(s东边缘边界高度)
{
高度=100;
宽度=100;
e、 HasMorePages=true;
返回;
}
高度+=dataGridView1.行[s]。高度;
如果(s%2==0)
{
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[0].Value.ToString(),fntString,Brush.Black,70,(s*89)+20);
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[1].Value.ToString(),fntString,Brushes.Black,100,(s*89)+20);
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[2].Value.ToString(),fntString,Brush.Black,70,(s*89)+40);
}
其他的
{//右列
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[0].Value.ToString(),fntString,Brush.Black,370,((s-1)*89)+20);
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[1].Value.ToString(),fntString,Brush.Black,400,((s-1)*89)+20);
e、 Graphics.DrawString(dataGridView1.Rows[s].Cells[2].Value.ToString(),fntString,Brush.Black,370,((s-1)*89)+40);
}
s++;
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
printDocument1.Print();
}
提议的答复
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int y = 0;
System.Drawing.Font fntString = new Font("Times New Roman", 10, FontStyle.Bold);
while (printIndex < dataGridView1.Rows.Count &&
y + dataGridView1.Rows[printIndex].Height < e.MarginBounds.Height)
{
// print your stuff, y is where you are on the page vertically
if (printIndex % 2 == 0)
{
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[0].Value.ToString(), fntString, Brushes.Black, 370, ((printIndex - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[1].Value.ToString(), fntString, Brushes.Black, 400, ((printIndex - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[2].Value.ToString(), fntString, Brushes.Black, 370, ((printIndex - 1) * 89) + 40);
}
else
{
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[0].Value.ToString(), fntString, Brushes.Black, 370, ((printIndex - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[1].Value.ToString(), fntString, Brushes.Black, 400, ((printIndex - 1) * 89) + 20);
e.Graphics.DrawString(dataGridView1.Rows[printIndex].Cells[2].Value.ToString(), fntString, Brushes.Black, 370, ((printIndex - 1) * 89) + 40);
}
y += dataGridView1.Rows[printIndex].Height;
++printIndex;
}
e.HasMorePages = printIndex < dataGridView1.Rows.Count;
}
private void printDocument1\u PrintPage(对象发送方,PrintPageEventArgs e)
{
int y=0;
System.Drawing.Font fntString=新字体(“Times new Roman”,10,FontStyle.Bold);
而(printIndex
PrintPage事件适用于要打印的每个页面。这意味着您的For…Each循环将无法工作,因为您正在告诉打印机打印当前页面上的所有内容 您必须在PrintPage方法作用域之外有一个变量,以跟踪索引的行
int printIndex;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
int y = 0;
while (printIndex < dataGridView1.Rows.Count &&
y + dataGridView1.Rows[printIndex].Height < e.MarginBounds.Height) {
// print your stuff, y is where you are on the page vertically
y += dataGridView1.Rows[printIndex].Height;
++printIndex;
}
e.HasMorePages = printIndex < dataGridView1.Rows.Count;
}
private void printDocument1_BeginPrint(object sender, PrintEventArgs e) {
printIndex = 0;
}