EDN Admin
Well-known member
Program error
Error 1 The best overloaded method match for gridPrinting.GridPrintingDemo.PrintGridDocument.PrintGridDocument(System.Windows.Forms.DataGrid) has some invalid arguments D:goldypendinggridPrintingForm1.cs 36 38 gridPrinting
Error 2 Argument 1: cannot convert from System.Windows.Forms.DataGridView to System.Windows.Forms.DataGrid D:goldypendinggridPrintingForm1.cs 36 61 gridPrinting
Error 3 The best overloaded method match for gridPrinting.GridPrintingDemo.PrintGridDocument.PrintGridDocument(System.Windows.Forms.DataGrid) has some invalid arguments D:goldypendinggridPrintingForm1.cs 60 38 gridPrinting
Error 4 Argument 1: cannot convert from System.Windows.Forms.DataGridView to System.Windows.Forms.DataGrid D:goldypendinggridPrintingForm1.cs 60 60 gridPrinting
this error found when rebuild the solution.
please dont give any link.
pls suggest code for the given files (what is the code & where to place)
i m giving code file & image file (all error in form1.cs)<img alt="" height="373" src="http://social.msdn.microsoft.com/Forums/getfile/153972" width="502
error on clicking printpriview & print option.
<pre class="prettyprint //Filename: form1.desiner.cs
namespace gridPrinting
{
partial class GridPrintingDemo
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GridPrintingDemo));
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.printPriviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pageSetupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.dGrid = new System.Windows.Forms.DataGridView();
this.studentNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentStdDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentSectionDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentMediumDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studClassInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.aDataSet = new gridPrinting.aDataSet();
this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
this.oPrintDialog = new System.Windows.Forms.PrintDialog();
this.oPageSetup = new System.Windows.Forms.PageSetupDialog();
this.studClassInfoTableAdapter = new gridPrinting.aDataSetTableAdapters.StudClassInfoTableAdapter();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dGrid)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.studClassInfoBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.aDataSet)).BeginInit();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(494, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem1,
this.printPriviewToolStripMenuItem,
this.pageSetupToolStripMenuItem,
this.printToolStripMenuItem,
this.toolStripSeparator1,
this.exitToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
this.fileToolStripMenuItem.Text = "File";
//
// fileToolStripMenuItem1
//
this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1";
this.fileToolStripMenuItem1.Size = new System.Drawing.Size(149, 22);
this.fileToolStripMenuItem1.Text = "&File";
//
// printPriviewToolStripMenuItem
//
this.printPriviewToolStripMenuItem.Name = "printPriviewToolStripMenuItem";
this.printPriviewToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.printPriviewToolStripMenuItem.Text = "Print Pri&view....";
this.printPriviewToolStripMenuItem.Click += new System.EventHandler(this.printPriviewToolStripMenuItem_Click);
//
// pageSetupToolStripMenuItem
//
this.pageSetupToolStripMenuItem.Name = "pageSetupToolStripMenuItem";
this.pageSetupToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.pageSetupToolStripMenuItem.Text = "Page &Setup...";
this.pageSetupToolStripMenuItem.Click += new System.EventHandler(this.pageSetupToolStripMenuItem_Click);
//
// printToolStripMenuItem
//
this.printToolStripMenuItem.Name = "printToolStripMenuItem";
this.printToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.printToolStripMenuItem.Text = "&Print...";
this.printToolStripMenuItem.Click += new System.EventHandler(this.printToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(146, 6);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.exitToolStripMenuItem.Text = "E&xit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
//
// dGrid
//
this.dGrid.AutoGenerateColumns = false;
this.dGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.studentNameDataGridViewTextBoxColumn,
this.studentStdDataGridViewTextBoxColumn,
this.studentSectionDataGridViewTextBoxColumn,
this.studentMediumDataGridViewTextBoxColumn});
this.dGrid.DataSource = this.studClassInfoBindingSource;
this.dGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.dGrid.Location = new System.Drawing.Point(0, 24);
this.dGrid.Name = "dGrid";
this.dGrid.Size = new System.Drawing.Size(494, 316);
this.dGrid.TabIndex = 0;
//
// studentNameDataGridViewTextBoxColumn
//
this.studentNameDataGridViewTextBoxColumn.DataPropertyName = "StudentName";
this.studentNameDataGridViewTextBoxColumn.HeaderText = "StudentName";
this.studentNameDataGridViewTextBoxColumn.Name = "studentNameDataGridViewTextBoxColumn";
//
// studentStdDataGridViewTextBoxColumn
//
this.studentStdDataGridViewTextBoxColumn.DataPropertyName = "StudentStd";
this.studentStdDataGridViewTextBoxColumn.HeaderText = "StudentStd";
this.studentStdDataGridViewTextBoxColumn.Name = "studentStdDataGridViewTextBoxColumn";
//
// studentSectionDataGridViewTextBoxColumn
//
this.studentSectionDataGridViewTextBoxColumn.DataPropertyName = "StudentSection";
this.studentSectionDataGridViewTextBoxColumn.HeaderText = "StudentSection";
this.studentSectionDataGridViewTextBoxColumn.Name = "studentSectionDataGridViewTextBoxColumn";
//
// studentMediumDataGridViewTextBoxColumn
//
this.studentMediumDataGridViewTextBoxColumn.DataPropertyName = "StudentMedium";
this.studentMediumDataGridViewTextBoxColumn.HeaderText = "StudentMedium";
this.studentMediumDataGridViewTextBoxColumn.Name = "studentMediumDataGridViewTextBoxColumn";
//
// studClassInfoBindingSource
//
this.studClassInfoBindingSource.DataMember = "StudClassInfo";
this.studClassInfoBindingSource.DataSource = this.aDataSet;
//
// aDataSet
//
this.aDataSet.DataSetName = "aDataSet";
this.aDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// ppDialog
//
this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
this.ppDialog.Enabled = true;
this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("ppDialog.Icon")));
this.ppDialog.Name = "ppDialog";
this.ppDialog.Visible = false;
//
// oPrintDialog
//
this.oPrintDialog.AllowSomePages = true;
this.oPrintDialog.UseEXDialog = true;
//
// studClassInfoTableAdapter
//
this.studClassInfoTableAdapter.ClearBeforeFill = true;
//
// GridPrintingDemo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(494, 340);
this.Controls.Add(this.dGrid);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "GridPrintingDemo";
this.Text = "Printing Grid Example";
this.Load += new System.EventHandler(this.GridPrintingDemo_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.studClassInfoBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.aDataSet)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
//stored page settings object
private System.Drawing.Printing.PageSettings oPageSettings;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem printPriviewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem pageSetupToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.DataGridView dGrid;
private System.Windows.Forms.PrintPreviewDialog ppDialog;
private System.Windows.Forms.PrintDialog oPrintDialog;
private System.Windows.Forms.PageSetupDialog oPageSetup;
private aDataSet aDataSet;
private System.Windows.Forms.BindingSource studClassInfoBindingSource;
private gridPrinting.aDataSetTableAdapters.StudClassInfoTableAdapter studClassInfoTableAdapter;
private System.Windows.Forms.DataGridViewTextBoxColumn studentNameDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentStdDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentSectionDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentMediumDataGridViewTextBoxColumn;
}
}[/code]
pls pls help me to resolve this problem & program run error free.
<pre class="prettyprint //FileName: form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
namespace gridPrinting
{
public partial class GridPrintingDemo : Form
{
public GridPrintingDemo()
{
InitializeComponent();
oPageSettings = new PageSettings();
}
private void GridPrintingDemo_Load(object sender, EventArgs e)
{
try
{
// TODO: This line of code loads data into the aDataSet.StudClassInfo table. You can move, or remove it, as needed.
this.studClassInfoTableAdapter.Fill(this.aDataSet.StudClassInfo);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
private void printPriviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//setup the document to print
PrintGridDocument aDoc = new PrintGridDocument(dGrid);
aDoc.Title = "Employee Report";
oPrintDialog.Document = aDoc;
if (oPrintDialog.ShowDialog() == DialogResult.OK)
{
//display the print priview dialog
aDoc.DefaultPageSettings = oPageSettings;
ppDialog.Document = aDoc;
ppDialog.ShowDialog();
}
}
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
oPageSetup.PageSettings = oPageSettings;
if (oPageSetup.ShowDialog() == DialogResult.OK)
{
oPageSettings = oPageSetup.PageSettings;
}
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
//Setup the document to print
PrintGridDocument aDoc = new PrintGridDocument(dGrid);
aDoc.Title = "Employee Report";
oPrintDialog.Document = aDoc;
if (oPrintDialog.ShowDialog() == DialogResult.OK)
{
//Display the print preview dialog
aDoc.DefaultPageSettings = oPageSettings;
try
{
//Print the document
aDoc.Print();
}
catch (Exception ex)
{
//Display any errors
MessageBox.Show(ex.ToString());
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public class PrintGridDocument : PrintDocument
{
//data member
private DataGrid m_oDataGrid;
private int m_nCurrPage;
private int m_nCurrRow;
private int m_nColumns;
private int m_nRows;
private bool m_nInitialized;
private int m_nLinesPerPage;
private int m_nTotalPages;
private int[] m_nColBounds;
//properties
public Font printFont;
public string Title;
public PrintGridDocument(DataGrid aGrid)
: base()
{
//default values
m_oDataGrid = aGrid;
m_nCurrPage = 0;
m_nCurrRow = 0;
m_nInitialized = false;
//get the total number of cols/rows in the data source
m_nColumns = ((DataTable)(m_oDataGrid.DataSource)).Columns.Count;
m_nRows = ((DataTable)(m_oDataGrid.DataSource)).Rows.Count;
}
//override OnBeginPrint to setup the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev)
{
//call base method
base.OnBeginPrint(ev);
//If client has not created a font, create a default font
// Note: an exception could be raised here, but it is deliberately not
// being caught because there is nothing we could do at this point!
if(printFont == null)
printFont = new Font("Arial", 9);
}
//Override the OnPrintPage to provide the printing logic for the document
protected override void OnPrintPage(PrintPageEventArgs e)
{
//call base method
base.OnPrintPage(e);
//get the margins
int nTextPosX = e.MarginBounds.Left;
int nTextPosY = e.MarginBounds.Top;
//do first time initialised stuff
if (!m_nInitialized)
{
//calculate the number of lines per page
m_nLinesPerPage = (int)(e.MarginBounds.Height / printFont.GetHeight(e.Graphics));
m_nTotalPages = (int)Math.Ceiling((float)m_nRows / (float)m_nLinesPerPage);
//create bounding box for the columns
m_nColBounds = new int[m_nColumns];
//calculate the correct spacing for the columns
for (int nCol = 0; nCol < m_nColumns; nCol++)
{
//measure the column header first
m_nColBounds[nCol] = (int)e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width;
for (int nRow = 0; nRow < m_nRows; nRow++)
{
//compare data to current max
if (e.Graphics.MeasureString(m_oDataGrid[nRow, nCol].ToString(), printFont).Width > m_nColBounds[nCol])
m_nColBounds[nCol] = (int)e.Graphics.MeasureString(m_oDataGrid[nRow, nCol].ToString(), printFont).Width;
}
//just use max possible size if too large
if (m_nColBounds[nCol] > e.MarginBounds.Width / m_nColumns)
m_nColBounds[nCol] = e.MarginBounds.Width / m_nColumns;
//Cant be less than column width
if (m_nColBounds[nCol] < (int)Math.Round(e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width))
m_nColBounds[nCol] = (int)Math.Round(e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width);
}
//Move to correct starting page
if (this.PrinterSettings.PrintRange == PrintRange.SomePages)
{
while (m_nCurrPage < this.PrinterSettings.FromPage - 1)
{
//Move to next page - advance data to next page as well
m_nCurrRow += m_nLinesPerPage;
m_nCurrPage++;
if (m_nCurrRow > m_nRows)
return;
}
if (m_nCurrPage > this.PrinterSettings.ToPage)
{
//Dont print anything more
return;
}
}
//set flag
m_nInitialized = true;
}
//move to next page
m_nCurrPage++;
//print title, if first page
if (m_nCurrPage == 1)
{
Font TitleFont = new Font("Arial", 15);
int nXPos = (int)(((e.PageBounds.Right - e.PageBounds.Left) / 2) -
(e.Graphics.MeasureString(Title, TitleFont).Width / 2));
e.Graphics.DrawString(Title, TitleFont, Brushes.Black, nXPos, e.MarginBounds.Top - TitleFont.GetHeight(e.Graphics) - 10);
}
//Draw Page Number
string strOutput = "Page " + m_nCurrPage + " of " + m_nTotalPages;
e.Graphics.DrawString(strOutput, printFont, Brushes.Black, e.MarginBounds.Right - e.Graphics.MeasureString(strOutput, printFont).Width,
e.MarginBounds.Bottom);
//Utility rectangle - use for many drawing operations
Rectangle aRect = new Rectangle();
//Loop through data
for (int nRow = m_nCurrRow; nRow < m_nRows; nRow++)
{
//Draw the current row within a shaded/unshaded box
aRect.X = e.MarginBounds.Left;
aRect.Y = nTextPosY;
aRect.Width = e.MarginBounds.Width;
aRect.Height = (int)printFont.GetHeight(e.Graphics);
//Draw the box
if (nRow % 2 == 0)
e.Graphics.FillRectangle(Brushes.LightGray, aRect);
e.Graphics.DrawRectangle(Pens.Black, aRect);
//Loop through each column
for (int nCol = 0; nCol < m_nColumns; nCol++)
{
//Set the rectangle to the correct position
aRect.X = nTextPosX;
aRect.Y = nTextPosY;
aRect.Width = m_nColBounds[nCol];
aRect.Height = (int)printFont.GetHeight(e.Graphics);
//Print the data
e.Graphics.DrawString(m_oDataGrid[nRow, nCol].ToString(), printFont, Brushes.Black, aRect);
//Advance the x Position counter
nTextPosX += m_nColBounds[nCol];
}
//Reassign the x position counter
nTextPosX = e.MarginBounds.Left;
//Move the y position counter down a line
nTextPosY += (int)printFont.GetHeight(e.Graphics);
//Check to see if we have reached the line limit - move to a new page if so
if (nRow - ((m_nCurrPage - 1) * m_nLinesPerPage) == m_nLinesPerPage)
{
//Save the current row
m_nCurrRow = ++nRow;
e.HasMorePages = true;
return;
}
}
}
}
}
}[/code]
thanks to all who solve & read this
View the full article
Error 1 The best overloaded method match for gridPrinting.GridPrintingDemo.PrintGridDocument.PrintGridDocument(System.Windows.Forms.DataGrid) has some invalid arguments D:goldypendinggridPrintingForm1.cs 36 38 gridPrinting
Error 2 Argument 1: cannot convert from System.Windows.Forms.DataGridView to System.Windows.Forms.DataGrid D:goldypendinggridPrintingForm1.cs 36 61 gridPrinting
Error 3 The best overloaded method match for gridPrinting.GridPrintingDemo.PrintGridDocument.PrintGridDocument(System.Windows.Forms.DataGrid) has some invalid arguments D:goldypendinggridPrintingForm1.cs 60 38 gridPrinting
Error 4 Argument 1: cannot convert from System.Windows.Forms.DataGridView to System.Windows.Forms.DataGrid D:goldypendinggridPrintingForm1.cs 60 60 gridPrinting
this error found when rebuild the solution.
please dont give any link.
pls suggest code for the given files (what is the code & where to place)
i m giving code file & image file (all error in form1.cs)<img alt="" height="373" src="http://social.msdn.microsoft.com/Forums/getfile/153972" width="502
error on clicking printpriview & print option.
<pre class="prettyprint //Filename: form1.desiner.cs
namespace gridPrinting
{
partial class GridPrintingDemo
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GridPrintingDemo));
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.printPriviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pageSetupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.dGrid = new System.Windows.Forms.DataGridView();
this.studentNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentStdDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentSectionDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studentMediumDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.studClassInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.aDataSet = new gridPrinting.aDataSet();
this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
this.oPrintDialog = new System.Windows.Forms.PrintDialog();
this.oPageSetup = new System.Windows.Forms.PageSetupDialog();
this.studClassInfoTableAdapter = new gridPrinting.aDataSetTableAdapters.StudClassInfoTableAdapter();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dGrid)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.studClassInfoBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.aDataSet)).BeginInit();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(494, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem1,
this.printPriviewToolStripMenuItem,
this.pageSetupToolStripMenuItem,
this.printToolStripMenuItem,
this.toolStripSeparator1,
this.exitToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
this.fileToolStripMenuItem.Text = "File";
//
// fileToolStripMenuItem1
//
this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1";
this.fileToolStripMenuItem1.Size = new System.Drawing.Size(149, 22);
this.fileToolStripMenuItem1.Text = "&File";
//
// printPriviewToolStripMenuItem
//
this.printPriviewToolStripMenuItem.Name = "printPriviewToolStripMenuItem";
this.printPriviewToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.printPriviewToolStripMenuItem.Text = "Print Pri&view....";
this.printPriviewToolStripMenuItem.Click += new System.EventHandler(this.printPriviewToolStripMenuItem_Click);
//
// pageSetupToolStripMenuItem
//
this.pageSetupToolStripMenuItem.Name = "pageSetupToolStripMenuItem";
this.pageSetupToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.pageSetupToolStripMenuItem.Text = "Page &Setup...";
this.pageSetupToolStripMenuItem.Click += new System.EventHandler(this.pageSetupToolStripMenuItem_Click);
//
// printToolStripMenuItem
//
this.printToolStripMenuItem.Name = "printToolStripMenuItem";
this.printToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.printToolStripMenuItem.Text = "&Print...";
this.printToolStripMenuItem.Click += new System.EventHandler(this.printToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(146, 6);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(149, 22);
this.exitToolStripMenuItem.Text = "E&xit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
//
// dGrid
//
this.dGrid.AutoGenerateColumns = false;
this.dGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.studentNameDataGridViewTextBoxColumn,
this.studentStdDataGridViewTextBoxColumn,
this.studentSectionDataGridViewTextBoxColumn,
this.studentMediumDataGridViewTextBoxColumn});
this.dGrid.DataSource = this.studClassInfoBindingSource;
this.dGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.dGrid.Location = new System.Drawing.Point(0, 24);
this.dGrid.Name = "dGrid";
this.dGrid.Size = new System.Drawing.Size(494, 316);
this.dGrid.TabIndex = 0;
//
// studentNameDataGridViewTextBoxColumn
//
this.studentNameDataGridViewTextBoxColumn.DataPropertyName = "StudentName";
this.studentNameDataGridViewTextBoxColumn.HeaderText = "StudentName";
this.studentNameDataGridViewTextBoxColumn.Name = "studentNameDataGridViewTextBoxColumn";
//
// studentStdDataGridViewTextBoxColumn
//
this.studentStdDataGridViewTextBoxColumn.DataPropertyName = "StudentStd";
this.studentStdDataGridViewTextBoxColumn.HeaderText = "StudentStd";
this.studentStdDataGridViewTextBoxColumn.Name = "studentStdDataGridViewTextBoxColumn";
//
// studentSectionDataGridViewTextBoxColumn
//
this.studentSectionDataGridViewTextBoxColumn.DataPropertyName = "StudentSection";
this.studentSectionDataGridViewTextBoxColumn.HeaderText = "StudentSection";
this.studentSectionDataGridViewTextBoxColumn.Name = "studentSectionDataGridViewTextBoxColumn";
//
// studentMediumDataGridViewTextBoxColumn
//
this.studentMediumDataGridViewTextBoxColumn.DataPropertyName = "StudentMedium";
this.studentMediumDataGridViewTextBoxColumn.HeaderText = "StudentMedium";
this.studentMediumDataGridViewTextBoxColumn.Name = "studentMediumDataGridViewTextBoxColumn";
//
// studClassInfoBindingSource
//
this.studClassInfoBindingSource.DataMember = "StudClassInfo";
this.studClassInfoBindingSource.DataSource = this.aDataSet;
//
// aDataSet
//
this.aDataSet.DataSetName = "aDataSet";
this.aDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// ppDialog
//
this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
this.ppDialog.Enabled = true;
this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("ppDialog.Icon")));
this.ppDialog.Name = "ppDialog";
this.ppDialog.Visible = false;
//
// oPrintDialog
//
this.oPrintDialog.AllowSomePages = true;
this.oPrintDialog.UseEXDialog = true;
//
// studClassInfoTableAdapter
//
this.studClassInfoTableAdapter.ClearBeforeFill = true;
//
// GridPrintingDemo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(494, 340);
this.Controls.Add(this.dGrid);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "GridPrintingDemo";
this.Text = "Printing Grid Example";
this.Load += new System.EventHandler(this.GridPrintingDemo_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.studClassInfoBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.aDataSet)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
//stored page settings object
private System.Drawing.Printing.PageSettings oPageSettings;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem printPriviewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem pageSetupToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.DataGridView dGrid;
private System.Windows.Forms.PrintPreviewDialog ppDialog;
private System.Windows.Forms.PrintDialog oPrintDialog;
private System.Windows.Forms.PageSetupDialog oPageSetup;
private aDataSet aDataSet;
private System.Windows.Forms.BindingSource studClassInfoBindingSource;
private gridPrinting.aDataSetTableAdapters.StudClassInfoTableAdapter studClassInfoTableAdapter;
private System.Windows.Forms.DataGridViewTextBoxColumn studentNameDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentStdDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentSectionDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn studentMediumDataGridViewTextBoxColumn;
}
}[/code]
pls pls help me to resolve this problem & program run error free.
<pre class="prettyprint //FileName: form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
namespace gridPrinting
{
public partial class GridPrintingDemo : Form
{
public GridPrintingDemo()
{
InitializeComponent();
oPageSettings = new PageSettings();
}
private void GridPrintingDemo_Load(object sender, EventArgs e)
{
try
{
// TODO: This line of code loads data into the aDataSet.StudClassInfo table. You can move, or remove it, as needed.
this.studClassInfoTableAdapter.Fill(this.aDataSet.StudClassInfo);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
private void printPriviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//setup the document to print
PrintGridDocument aDoc = new PrintGridDocument(dGrid);
aDoc.Title = "Employee Report";
oPrintDialog.Document = aDoc;
if (oPrintDialog.ShowDialog() == DialogResult.OK)
{
//display the print priview dialog
aDoc.DefaultPageSettings = oPageSettings;
ppDialog.Document = aDoc;
ppDialog.ShowDialog();
}
}
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
oPageSetup.PageSettings = oPageSettings;
if (oPageSetup.ShowDialog() == DialogResult.OK)
{
oPageSettings = oPageSetup.PageSettings;
}
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
//Setup the document to print
PrintGridDocument aDoc = new PrintGridDocument(dGrid);
aDoc.Title = "Employee Report";
oPrintDialog.Document = aDoc;
if (oPrintDialog.ShowDialog() == DialogResult.OK)
{
//Display the print preview dialog
aDoc.DefaultPageSettings = oPageSettings;
try
{
//Print the document
aDoc.Print();
}
catch (Exception ex)
{
//Display any errors
MessageBox.Show(ex.ToString());
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public class PrintGridDocument : PrintDocument
{
//data member
private DataGrid m_oDataGrid;
private int m_nCurrPage;
private int m_nCurrRow;
private int m_nColumns;
private int m_nRows;
private bool m_nInitialized;
private int m_nLinesPerPage;
private int m_nTotalPages;
private int[] m_nColBounds;
//properties
public Font printFont;
public string Title;
public PrintGridDocument(DataGrid aGrid)
: base()
{
//default values
m_oDataGrid = aGrid;
m_nCurrPage = 0;
m_nCurrRow = 0;
m_nInitialized = false;
//get the total number of cols/rows in the data source
m_nColumns = ((DataTable)(m_oDataGrid.DataSource)).Columns.Count;
m_nRows = ((DataTable)(m_oDataGrid.DataSource)).Rows.Count;
}
//override OnBeginPrint to setup the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev)
{
//call base method
base.OnBeginPrint(ev);
//If client has not created a font, create a default font
// Note: an exception could be raised here, but it is deliberately not
// being caught because there is nothing we could do at this point!
if(printFont == null)
printFont = new Font("Arial", 9);
}
//Override the OnPrintPage to provide the printing logic for the document
protected override void OnPrintPage(PrintPageEventArgs e)
{
//call base method
base.OnPrintPage(e);
//get the margins
int nTextPosX = e.MarginBounds.Left;
int nTextPosY = e.MarginBounds.Top;
//do first time initialised stuff
if (!m_nInitialized)
{
//calculate the number of lines per page
m_nLinesPerPage = (int)(e.MarginBounds.Height / printFont.GetHeight(e.Graphics));
m_nTotalPages = (int)Math.Ceiling((float)m_nRows / (float)m_nLinesPerPage);
//create bounding box for the columns
m_nColBounds = new int[m_nColumns];
//calculate the correct spacing for the columns
for (int nCol = 0; nCol < m_nColumns; nCol++)
{
//measure the column header first
m_nColBounds[nCol] = (int)e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width;
for (int nRow = 0; nRow < m_nRows; nRow++)
{
//compare data to current max
if (e.Graphics.MeasureString(m_oDataGrid[nRow, nCol].ToString(), printFont).Width > m_nColBounds[nCol])
m_nColBounds[nCol] = (int)e.Graphics.MeasureString(m_oDataGrid[nRow, nCol].ToString(), printFont).Width;
}
//just use max possible size if too large
if (m_nColBounds[nCol] > e.MarginBounds.Width / m_nColumns)
m_nColBounds[nCol] = e.MarginBounds.Width / m_nColumns;
//Cant be less than column width
if (m_nColBounds[nCol] < (int)Math.Round(e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width))
m_nColBounds[nCol] = (int)Math.Round(e.Graphics.MeasureString(((DataTable)(m_oDataGrid.DataSource)).Columns[nCol].ColumnName, printFont).Width);
}
//Move to correct starting page
if (this.PrinterSettings.PrintRange == PrintRange.SomePages)
{
while (m_nCurrPage < this.PrinterSettings.FromPage - 1)
{
//Move to next page - advance data to next page as well
m_nCurrRow += m_nLinesPerPage;
m_nCurrPage++;
if (m_nCurrRow > m_nRows)
return;
}
if (m_nCurrPage > this.PrinterSettings.ToPage)
{
//Dont print anything more
return;
}
}
//set flag
m_nInitialized = true;
}
//move to next page
m_nCurrPage++;
//print title, if first page
if (m_nCurrPage == 1)
{
Font TitleFont = new Font("Arial", 15);
int nXPos = (int)(((e.PageBounds.Right - e.PageBounds.Left) / 2) -
(e.Graphics.MeasureString(Title, TitleFont).Width / 2));
e.Graphics.DrawString(Title, TitleFont, Brushes.Black, nXPos, e.MarginBounds.Top - TitleFont.GetHeight(e.Graphics) - 10);
}
//Draw Page Number
string strOutput = "Page " + m_nCurrPage + " of " + m_nTotalPages;
e.Graphics.DrawString(strOutput, printFont, Brushes.Black, e.MarginBounds.Right - e.Graphics.MeasureString(strOutput, printFont).Width,
e.MarginBounds.Bottom);
//Utility rectangle - use for many drawing operations
Rectangle aRect = new Rectangle();
//Loop through data
for (int nRow = m_nCurrRow; nRow < m_nRows; nRow++)
{
//Draw the current row within a shaded/unshaded box
aRect.X = e.MarginBounds.Left;
aRect.Y = nTextPosY;
aRect.Width = e.MarginBounds.Width;
aRect.Height = (int)printFont.GetHeight(e.Graphics);
//Draw the box
if (nRow % 2 == 0)
e.Graphics.FillRectangle(Brushes.LightGray, aRect);
e.Graphics.DrawRectangle(Pens.Black, aRect);
//Loop through each column
for (int nCol = 0; nCol < m_nColumns; nCol++)
{
//Set the rectangle to the correct position
aRect.X = nTextPosX;
aRect.Y = nTextPosY;
aRect.Width = m_nColBounds[nCol];
aRect.Height = (int)printFont.GetHeight(e.Graphics);
//Print the data
e.Graphics.DrawString(m_oDataGrid[nRow, nCol].ToString(), printFont, Brushes.Black, aRect);
//Advance the x Position counter
nTextPosX += m_nColBounds[nCol];
}
//Reassign the x position counter
nTextPosX = e.MarginBounds.Left;
//Move the y position counter down a line
nTextPosY += (int)printFont.GetHeight(e.Graphics);
//Check to see if we have reached the line limit - move to a new page if so
if (nRow - ((m_nCurrPage - 1) * m_nLinesPerPage) == m_nLinesPerPage)
{
//Save the current row
m_nCurrRow = ++nRow;
e.HasMorePages = true;
return;
}
}
}
}
}
}[/code]
thanks to all who solve & read this
View the full article