سلام دوستان
بنده یه تیکه کد نوشتم که اطلاعات گرید ویو را برای چاپ ارسال می کند به پرینتر
همه چیز اکی هست تا زمانی که اطلاعات بیش از یک صفحه شود! وقتی اطلاعات به صفحه دوم می رود همان اطلاعات صفحه اول را چاپ می کند. خیلی سروکله زدم اما نتوانستم بفهمم چرا همش از صفحه اول کپی می کند
لطفا راهنمایی فرمایید
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Service { public class Print { System.Drawing.Printing.PrintDocument printDocumnet = new System.Drawing.Printing.PrintDocument(); DataGridView __myGridView; string _printTitle; //int x = 50; //int y = 130; // int gridWidth; Font oFont = new Font("B Traffic", 11); DataGridViewButtonColumn oButton = new DataGridViewButtonColumn(); public Print(DataGridView myGrid, string printTitle) { __myGridView = myGrid; _printTitle = printTitle; } private void CreateGridViewContent(object sender, System.Drawing.Printing.PrintPageEventArgs e) { } public void PrintPageFromDataGridView(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //============================ create gridview header int x = 50; int y = 130; int gridWidth = 0; for (int j = this.__myGridView.Columns.Count - 1; j > -1; j--) { if (this.__myGridView.Columns[j].HeaderText != null && this.__myGridView.Columns[j].CellType != oButton.CellType) { Rectangle rect = new Rectangle(x, y, this.__myGridView.Columns[j].Width, this.__myGridView.ColumnHeadersHeight); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; e.Graphics.FillRectangle(Brushes.LightGray, rect); e.Graphics.DrawRectangle(Pens.Black, rect); e.Graphics.DrawString(this.__myGridView.Columns[j].HeaderText, oFont, Brushes.Black, rect, sf); x += rect.Width; gridWidth = gridWidth + this.__myGridView.Columns[j].Width; } } //============================ create page header Rectangle rectHeader = new Rectangle(50, 80, gridWidth, 30); StringFormat paperHeader = new StringFormat(); paperHeader.LineAlignment = StringAlignment.Center; paperHeader.Alignment = StringAlignment.Center; e.Graphics.FillRectangle(Brushes.LightGray, rectHeader); e.Graphics.DrawRectangle(Pens.Black, rectHeader); e.Graphics.DrawString(_printTitle, oFont, Brushes.Black, rectHeader, paperHeader); //==================================== content of gridview x = 50; y += this.__myGridView.ColumnHeadersHeight; foreach (DataGridViewRow row in this.__myGridView.Rows) { if (row.Cells[0].Value != null) { for (int j = this.__myGridView.Columns.Count - 1; j > -1; j--) { DataGridViewCell cell; cell = row.Cells[j]; if (cell.Value != null && cell.GetType() != oButton.CellType) { Rectangle rect = new Rectangle(x, y, cell.Size.Width, cell.Size.Height); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; e.Graphics.DrawRectangle(Pens.Black, rect); e.Graphics.DrawString(cell.Value.ToString(), oFont, Brushes.Black, rect, sf); x += rect.Width; } } x = 50; y += row.Height; if (y > e.MarginBounds.Height) { e.HasMorePages = true; y = 130; //=========================== CreateGridViewHeader(sender, e); //for (int j = this.__myGridView.Columns.Count - 1; j > -1; j--) //{ // if (this.__myGridView.Columns[j].HeaderText != null && this.__myGridView.Columns[j].CellType != oButton.CellType) // { // Rectangle rect = new Rectangle(x, y, // this.__myGridView.Columns[j].Width, // this.__myGridView.ColumnHeadersHeight); // StringFormat sf = new StringFormat(); // sf.LineAlignment = StringAlignment.Center; // sf.Alignment = StringAlignment.Center; // e.Graphics.FillRectangle(Brushes.LightGray, rect); // e.Graphics.DrawRectangle(Pens.Black, rect); // e.Graphics.DrawString(this.__myGridView.Columns[j].HeaderText, // oFont, Brushes.Black, rect, sf); // x += rect.Width; // gridWidth = gridWidth + this.__myGridView.Columns[j].Width; // } //} return; } else { e.HasMorePages = false; } } } } public void ShowPrintDialog() { PrintDialog pDialog = new PrintDialog(); pDialog.AllowCurrentPage = false; pDialog.AllowPrintToFile = true; pDialog.AllowSelection = false; pDialog.AllowSomePages = false; pDialog.PrintToFile = false; pDialog.ShowHelp = false; pDialog.ShowNetwork = false; if (pDialog.ShowDialog() == DialogResult.OK) { printDocumnet.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPageFromDataGridView); printDocumnet.DocumentName = _printTitle; printDocumnet.PrinterSettings = pDialog.PrinterSettings; printDocumnet.DefaultPageSettings = pDialog.PrinterSettings.DefaultPageSettings; printDocumnet.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(20, 20, 60, 20); PrintPreviewDialog oPrintDialog = new PrintPreviewDialog(); oPrintDialog.Document = printDocumnet; oPrintDialog.Show(); } } } }
بهتره از StumulSoft برای گزارش استفاده کنید
خیلی ساده و قدرتمند است
ممنون جناب مدائنی
فقط یک سوال؟!
من الان از استیمول استفاده کردم و گزارش خودم را گرفتم منتها وقتی اپلیکیشن ویندوزی را سمت کلاینت می برم ارور می دهد که استیمول پیدا نمی شود
مگر باید سمت کلاینت هم نصب بشه؟!؟!؟!
خیر نیازی به نصب نیست
فقط کافیست Dll های مربوط بهش در کنار برنامه باشه
ممنون از راهنمایی ارزشمندتون
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)