2017-05-05 77 views
0

大家好我寫了一個代碼來打印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://stackoverflow.com/questions/27448856/how-按照給定的答案嗨按照更新問題,我已經嘗試,但我沒有看到任何數據在第二頁,甚至是數據正在合併 – Dotnet

回答

1

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