打印问题与datagridview winforms

问题描述:

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

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

+0

https://www.codeproject.com/Articles/42925/Printing-Multiple-Pages-and-Tabular-Printing –

+0

我想你可以试试这段代码http://*.com/questions/27448856/how-按照给定的答案嗨按照更新问题,我已经尝试,但我没有看到任何数据在第二页,甚至是数据正在合并 – Dotnet

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

使用BeginPrint事件重置printIndex值:

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

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

+0

嗨巴拉一个小的帮助,我想打印左边的奇数行,甚至右边的行,我如何更改此代码 – Nithya

+0

嗨巴拉在打印的值datagridview-in-c – Nithya