datagridview winforms打印问题

时间:2017-05-05 05:33:56

标签: c# winforms datagridview

大家好我已经编写了一个代码来打印datagridview行,它工作正常但我没有看到我的第二页中的数据可以帮助我一些,这里是我尝试的代码我有90行我尝试发表其他一些文章,但没有运气

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 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;
        }

enter image description here

1 个答案:

答案 0 :(得分:1)

PrintPage事件适用于您要打印的每个页面。这意味着你的For ...每个循环都不起作用,因为你告诉打印机打印当前页面上的所有内容。

您必须在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;
}

使用BeginPrint事件重置printIndex值:

private void printDocument1_BeginPrint(object sender, PrintEventArgs e) {
  printIndex = 0;
}

如果添加另一个变量(例如int printPage;),您现在可以通过在PrintPage事件中递增该值来了解当前正在打印的页面。