###前提・実現したいこと
foreachの読み取り位置を記憶して50行ずつ印刷したい
###発生している問題・エラーメッセージ
同じ50行が200ページ印刷されてしまう
###該当のソースコード
C#
1 /// <summary> 2 /// 降順印刷ボタンのイベントハンドラ 3 /// product_tableを降順に並べ替えて印刷 4 /// </summary> 5 /// <param name="sender">使用しない</param> 6 /// <param name="e">使用しない</param> 7 private void buttonDescendingOrderPrint_Click(object sender, EventArgs e) 8 { 9 string sql = @" 10 SELECT * 11 FROM product_table 12 ORDER BY product_id DESC;"; 13 14 using (connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 15 { 16 try 17 { 18 connection.Open(); 19 } 20 catch (Exception ex) 21 { 22 MessageBox.Show("データベースを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 23 System.Diagnostics.Debug.WriteLine(ex.ToString()); 24 } 25 26 List<ProductData> productList = new List<ProductData>(); 27 28 SqlCommand command = new SqlCommand(sql, connection); 29 using (SqlDataReader reader = command.ExecuteReader()) 30 { 31 while (reader.Read() == true) 32 { 33 ProductData productData = new ProductData(); 34 productData.ProductId = reader["product_id"].ToString(); 35 productData.BasicOrderLot = reader["basic_order_lot"].ToString(); 36 productData.ProductCode = reader["product_code"].ToString(); 37 productData.ChassisNumber = reader["chassis_number"].ToString(); 38 productData.AssembledYmd = reader["assembled_ymd"].ToString(); 39 productData.AssembledHms = reader["assembled_hms"].ToString(); 40 productData.StructNumber = reader["struct_number"].ToString(); 41 productData.YyyyMm = reader["yyyymm"].ToString(); 42 43 productList.Add(productData); 44 } 45 reader.Close(); 46 } 47 48 productDataBindingSource.DataSource = productList; 49 PrintOut(); 50 } 51 } 52 53 /// <summary> 54 /// PrintOutメソッド 55 /// 印刷ダイアログを表示して印刷する 56 /// </summary> 57 private void PrintOut() 58 { 59 using (PrintDocument document = new PrintDocument()) 60 { 61 document.PrintPage += new PrintPageEventHandler(PrintPage); 62 PrintDialog log = new PrintDialog(); 63 if (log.ShowDialog() == DialogResult.Cancel) 64 { 65 return; 66 } 67 document.Print(); 68 } 69 } 70 71 /// <summary> 72 /// 1ページ印刷するイベントハンドラ 73 /// 1ページ分の印刷設定 74 /// </summary> 75 /// <param name="sender">使用しない</param> 76 /// <param name="e">使用する</param> 77 private void PrintPage(object sender, PrintPageEventArgs e) 78 { 79 int line = 0; 80 int x, y = 20; 81 e.HasMorePages = true; 82 Font font = new Font("MS ゴシック", 12); 83 84 foreach (ProductData data in productList) 85 { 86 x = 30; 87 e.Graphics.DrawString(data.ProductId, font, Brushes.Black, x, y); 88 x = x + 80; 89 e.Graphics.DrawString(data.BasicOrderLot, font, Brushes.Black, x, y); 90 x = x + 90; 91 e.Graphics.DrawString(data.ProductCode, font, Brushes.Black, x, y); 92 x = x + 80; 93 e.Graphics.DrawString(data.ChassisNumber, font, Brushes.Black, x, y); 94 x = x + 60; 95 e.Graphics.DrawString(data.AssembledYmd, font, Brushes.Black, x, y); 96 x = x + 150; 97 e.Graphics.DrawString(data.AssembledHms, font, Brushes.Black, x, y); 98 x = x + 100; 99 e.Graphics.DrawString(data.StructNumber, font, Brushes.Black, x, y); 100 x = x + 100; 101 e.Graphics.DrawString(data.YyyyMm, font, Brushes.Black, x, y); 102 y = y + 20; 103 line++; 104 105 if (line == 50) 106 { 107 break; 108 } 109 } 110 111 e.Graphics.DrawString(String.Format("{0}ページ", page), new Font("MS ゴシック", 12) 112 , Brushes.Black, 365, 1110); 113 page++; 114 115 if (MAX_PAGE == page) 116 { 117 e.HasMorePages = false; 118 connection.Close(); 119 } 120 } 121 } 122}
###補足情報(言語/FW/ツール等のバージョンなど)
5852232 2A11B5632ZABFA157220000825 18:08:57 8D11080200008
このようなデータが1万行あります。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/13 07:59
2017/09/13 08:28
2017/09/13 08:32
2017/09/13 08:33
2017/09/13 08:37