Как раз мне надо было тоже самое! За 10 минут ковыряния я таки распечатал датагрид. Итак: 1. Вешаем на кнопку печати:
Код | private void button7_Click(object sender, EventArgs e) { MyDataGridView = d; // d-мой датагрид, который на форме if (SetupThePrinting()) MyPrintDocument.Print(); }
|
2. Вставляем этот код:
Код | DataGridView MyDataGridView = new DataGridView(); DataGridViewPrinter MyDataGridViewPrinter;
private bool SetupThePrinting() { PrintDialog MyPrintDialog = new PrintDialog(); MyPrintDialog.AllowCurrentPage = false; MyPrintDialog.AllowPrintToFile = false; MyPrintDialog.AllowSelection = false; MyPrintDialog.AllowSomePages = false; MyPrintDialog.PrintToFile = false; MyPrintDialog.ShowHelp = false; MyPrintDialog.ShowNetwork = false;
if (MyPrintDialog.ShowDialog() != DialogResult.OK) return false;
MyPrintDocument.DocumentName = "Customers Report"; MyPrintDocument.PrinterSettings = MyPrintDialog.PrinterSettings; MyPrintDocument.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings; MyPrintDocument.DefaultPageSettings.Margins = new Margins(40, 40, 40, 40);
if (MessageBox.Show("Do you want the report to be centered on the page", "InvoiceManager - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView, MyPrintDocument, true, true, "Отчет", new Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point), Color.Black, true); else MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView, MyPrintDocument, false, true, "Отчет", new Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point), Color.Black, true);
return true; } }
|
3. Кидаем на форму компанент PrintDocument, обзываем его "MyPrintDocument" и вешаем на событие "PrintPage" код:
Код | bool more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics); if (more == true) e.HasMorePages = true;
|
Вуаля. В 3-ем шаге сделал так, потому что у меня создаваемый кодом PrintDocument и обработчик события печати отказывались работать. |