Hey guys,


I have got a problem. I have an application that shows data in datagridview.
Now, I want to add 'Print' option in it. I want to it print only those that are
selected. I have tried a code but it didn't help at all. Please suggest me something
about.

Any kind of help is appreciated!


Thank you
Ajinkya

can you please show ur code..

Add a check box column in datagridview and on print button click print rows which are selected. In my work i also have to make report of certain rows and pint it i make all unselected rows invisible on print button click

Hey here's the code i am having problem in.

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

namespace print
{
    public partial class Form1 : Form
    {
        private PrintDocument printDocument1;
        private PrintPreviewDialog printPreviewDialog1;
        private PageSetupDialog pageSetupDialog1;

        public Form1()
        {
            InitializeComponent();
            printDocument1 = new PrintDocument();
           printDocument1.PrintPage +=new PrintPageEventHandler(printDocument1_PrintPage);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mISDataSet.transact1' table. You can move, or remove it, as needed.
            this.transact1TableAdapter.Fill(this.mISDataSet.transact1);
                
        }

        
        
        private void button1_Click(object sender, EventArgs e)
        {
            

            Form1 fpr = new Form1();
            fpr.Text = dataGridView1.Text;
            
            fpr.ShowDialog();
            if (fpr.Result > 0)
             {
               PrintGrid = new DataGridViewPrint(printDocument1, dataGridView1, fpr.bBlackWhite);
               PrintGrid.PrintTitle = fpr.bTitle;
               PrintGrid.Title = fpr.Title;
               if (fpr.Result == 1) // Print
                {
                  if (printDialog1.ShowDialog() == DialogResult.OK)
                   {
                       // The Print method prints the DataGridView without using a print dialog.
                       // Use a PrintDialog when you want to offer the user the ability to choose print settings.
                      printDocument1.Print();
                   }
                }
                else if (fpr.Result == 2) // Page setup
                {
                  pageSetupDialog1.ShowDialog();
                }
             }
             else if (fpr.Result == 3) // Preview
             {
                 printPreviewDialog1.Icon = fpr.Icon;
                 printPreviewDialog1.ShowDialog();
             }
         }


         // Specify the output to print by handling the PrintPage event
         // and by using the Graphics included in the PrintPageEventArgs.
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
               // Print method of DataGridViewPrint class starts the custom DataGridView's printing process.
            e.HasMorePages = PrintGrid.Print(e.Graphics);
        }
        
    }
}

I get lot of errors in this code. I think there's no 'DataGridViewPrint' available to access. Can anyone how can i do it?

I used this code once in a small project and it worked perfectly. Done a little modification, but that had to do with what I wanted to do, not because the code was bad.

commented: Good link & suggestion too :) +12

Hey Ddanbe,
I am extremely thankful for your reply. It really helped me with most of my problem, except for one. This code prints all text in portrait format. I want it to be printed in landscape format. I tried making some adjustments in the code but it didn't work.
What do you think i should do in this case.

Regards
Ajinkya

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.