List of Exercises

How to Print a DataGridView in C# WinForms

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Print_Dialog_with_Preview
{
  public partial class Form1 : Form
  {
    private PrintDocument printDocument1 = new PrintDocument();
    private string documentContents;
    public string stringToPrint;
    private PrintDialog printDialog1;
    public Form1()
    {
      InitializeComponent();
      printDocument1.PrintPage +=
       new PrintPageEventHandler(printDocument1_PrintPage);
      printDialog1 = new PrintDialog();
    }

    private void Print_Preview_Click(object sender, EventArgs e)
    {
      stringToPrint = Data_Grid_to_String(dataGridView1, ',');
      printDocument1.DocumentName = stringToPrint;
      printPreviewDialog1.Document = printDocument1;
      ToolStripButton b = new ToolStripButton();
      b.Image = Properties.Resources.print1;
      b.DisplayStyle = ToolStripItemDisplayStyle.Image;
      b.Click += printPreview_PrintClick;
      ((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
      ((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);

      printPreviewDialog1.ShowDialog();
    }

    private void printPreview_PrintClick(object sender, EventArgs e)
    {
      try
      {
        DialogResult result = printDialog1.ShowDialog();
        // If the result is OK then print the document.
        if (result == DialogResult.OK)
        {
          stringToPrint = printDocument1.DocumentName;
          printDocument1.PrinterSettings = printDialog1.PrinterSettings;
          printDocument1.Print();
          MessageBox.Show("Is printed!");
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, ToString());
      }
    }
    public static string Data_Grid_to_String(DataGridView dgv, char delimiter)
    {
      StringBuilder sb = new StringBuilder();
      foreach (DataGridViewRow row in dgv.Rows)
      {
        foreach (DataGridViewCell cell in row.Cells)
        {
          sb.Append(cell.Value);
          sb.Append(delimiter);
          sb.Append(" ");
        }
        sb.Remove(sb.Length - 2, 2); // Removes the last delimiter
        sb.AppendLine();
      }
      return sb.ToString();
    }

    void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
      int charactersOnPage = 0;
      int linesPerPage = 0;

      // Sets the value of charactersOnPage to the number of characters
      // of stringToPrint that will fit within the bounds of the page.
      e.Graphics.MeasureString(stringToPrint, this.Font,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);

      // Draws the string within the bounds of the page.
      e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
      e.MarginBounds, StringFormat.GenericTypographic);

      // Remove the portion of the string that has been printed.
      stringToPrint = stringToPrint.Substring(charactersOnPage);

      // Check to see if more pages are to be printed.
      e.HasMorePages = (stringToPrint.Length > 0);

      // If there are no more pages, reset the string to be printed.
      if (!e.HasMorePages)
        stringToPrint = documentContents;
    }
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Print a DataGridView in C# | Build a PrintPreviewDialog, PrintDialog in Winforms


Video Thumbnail