hallaji
جمعه 04 مرداد 1387, 00:43 صبح
با عرض سلام خدمت دوستان عزيز و برنامه نويسان گرامي
كلاس DataGridViewPrinter.cs من كه عمل چاپ كردن ديتا گريدويو را انجام ميدهد بصورت زير است
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
class DataGridViewPrinter
{
private DataGridView TheDataGridView; // The DataGridView Control which will be printed
private PrintDocument ThePrintDocument; // The PrintDocument to be used for printing
private bool IsCenterOnPage; // Determine if the report will be printed in the Top-Center of the page
private bool IsWithTitle; // Determine if the page contain title text
private string TheTitleText; // The title text to be printed in each page (if IsWithTitle is set to true)
private Font TheTitleFont; // The font to be used with the title text (if IsWithTitle is set to true)
private Color TheTitleColor; // The color to be used with the title text (if IsWithTitle is set to true)
private bool IsWithPaging; // Determine if paging is used
private bool IsWithz;
static int CurrentRow; // A static parameter that keep track on which Row (in the DataGridView control) that should be printed
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY; // A parameter that keep track on the y coordinate of the page, so the next object to be printed will start from this y coordinate
private float CurrentZ;
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
// Maintain a generic list to hold start/stop points for the column printing
// This will be used for wrapping in situations where the DataGridView will not fit on a single page
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
// The class constructor
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging,bool WithZ)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
IsWithz = WithZ;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
// Claculating the PageWidth and the PageHeight
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
// Claculating the page margins
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right ;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Botto m;
// First, the current row to be printed is the first row in the DataGridView control
CurrentRow = 0;
}
// The function that calculate the height of each row (including the header row), the width of each column (according to the longest text in all its cells including the header cell), and the whole DataGridView width
private void Calculate(Graphics g)
{
if (PageNumber == 0) // Just calculate once
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView.Columns.Count; i++)
{
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font ;
if (tmpFont == null) // If there is no special HeaderFont style, then use the default DataGridView font style
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
tmpWidth = 220; ////tmpWidth = tmpSize.Width;
RowHeaderHeight = 40;////RowHeaderHeight = tmpSize.Height;
for (int j = 0; j < TheDataGridView.Rows.Count; j++)
{
tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
if (tmpFont == null) // If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString("Anything", tmpFont);
RowsHeight.Add(tmpSize.Height);
tmpSize = g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
if (tmpSize.Width > tmpWidth)
tmpWidth = tmpSize.Width;
}
if (TheDataGridView.Columns[i].Visible)
TheDataGridViewWidth += tmpWidth;
ColumnsWidth.Add(tmpWidth);
}
// Define the start/stop column points based on the page width and the DataGridView Width
// We will use this to determine the columns which are drawn on each page and how wrapping will be handled
// By default, the wrapping will occurr such that the maximum number of columns for a page will be determine
int k;
int mStartPoint = 0;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
if (TheDataGridView.Columns[k].Visible)
{
mStartPoint = k;
break;
}
int mEndPoint = TheDataGridView.Columns.Count;
for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
if (TheDataGridView.Columns[k].Visible)
{
mEndPoint = k + 1;
break;
}
float mTempWidth = TheDataGridViewWidth;
float mTempPrintArea = (float)PageWidth - (float)LeftMargin - (float)RightMargin;
// We only care about handling where the total datagridview width is bigger then the print area
if (TheDataGridViewWidth > mTempPrintArea)
{
mTempWidth = 0.0F;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
{
if (TheDataGridView.Columns[k].Visible)
{
mTempWidth += ColumnsWidth[k];
// If the width is bigger than the page area, then define a new column print range
if (mTempWidth > mTempPrintArea)
{
mTempWidth -= ColumnsWidth[k];
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mStartPoint = k;
mTempWidth = ColumnsWidth[k];
}
}
// Our end point is actually one index above the current index
mEndPoint = k + 1;
}
}
// Add the last set of columns
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mColumnPoint = 0;
}
}
// The funtion that print the title, page number, and the header row
private void DrawHeader(Graphics g)
{
CurrentY = (float)TopMargin;
CurrentZ = (float) TopMargin;
// Printing the page number (if isWithPaging is set to true)
if (IsWithPaging)
{
PageNumber++;
string PageString = "شركت سهامي .......
StringFormat PageStringFormat = new StringFormat();
PageStringFormat.Trimming = StringTrimming.Word;
PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
PageStringFormat.Alignment = StringAlignment.Center;
Font PageStringFont = new Font("Zar Mazar", 20, FontStyle.Bold, GraphicsUnit.Point);
RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, PageStringFont).Height);
g.DrawString(PageString, PageStringFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);
CurrentY += g.MeasureString(PageString, PageStringFont).Height;
}
/////////////////////////////////////////////
if (IsWithz)
{
string ZPageString ="\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n: صاحب جمع اموال : امور اداري ناظر: تحويل گيرنده ";
StringFormat ZStringFormat = new StringFormat();
ZStringFormat.Trimming = StringTrimming.Word;
ZStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
ZStringFormat.Alignment = StringAlignment.Far;
Font ZPageStringFont = new Font("Zar Mazar", 14, FontStyle.Bold, GraphicsUnit.Point);
RectangleF ZPageSringRectangle = new RectangleF((float)LeftMargin, CurrentZ, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(ZPageString,ZPageStringFont).Heigh t);
g.DrawString(ZPageString, ZPageStringFont, new SolidBrush(Color.Black), ZPageSringRectangle, ZStringFormat);
CurrentZ -= g.MeasureString(ZPageString, ZPageStringFont).Height;
}
////////////////////////////////////////////////
// Printing the title (if IsWithTitle is set to true)
if (IsWithTitle)
حالا سوالي كه داشتم اينه چطور ميتوانم مقدار pagestring را از textbox1.text فرم 1 بگيرم
(نتيجه جستجوهاي بنده استفاده از متغيير سراسري بين چند فرم بود)
--------------------------------------------------------------------------------------
هيچ جايي مثله برنامه نويسي نميشه
كلاس DataGridViewPrinter.cs من كه عمل چاپ كردن ديتا گريدويو را انجام ميدهد بصورت زير است
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
class DataGridViewPrinter
{
private DataGridView TheDataGridView; // The DataGridView Control which will be printed
private PrintDocument ThePrintDocument; // The PrintDocument to be used for printing
private bool IsCenterOnPage; // Determine if the report will be printed in the Top-Center of the page
private bool IsWithTitle; // Determine if the page contain title text
private string TheTitleText; // The title text to be printed in each page (if IsWithTitle is set to true)
private Font TheTitleFont; // The font to be used with the title text (if IsWithTitle is set to true)
private Color TheTitleColor; // The color to be used with the title text (if IsWithTitle is set to true)
private bool IsWithPaging; // Determine if paging is used
private bool IsWithz;
static int CurrentRow; // A static parameter that keep track on which Row (in the DataGridView control) that should be printed
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY; // A parameter that keep track on the y coordinate of the page, so the next object to be printed will start from this y coordinate
private float CurrentZ;
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
// Maintain a generic list to hold start/stop points for the column printing
// This will be used for wrapping in situations where the DataGridView will not fit on a single page
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
// The class constructor
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging,bool WithZ)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
IsWithz = WithZ;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
// Claculating the PageWidth and the PageHeight
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
// Claculating the page margins
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right ;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Botto m;
// First, the current row to be printed is the first row in the DataGridView control
CurrentRow = 0;
}
// The function that calculate the height of each row (including the header row), the width of each column (according to the longest text in all its cells including the header cell), and the whole DataGridView width
private void Calculate(Graphics g)
{
if (PageNumber == 0) // Just calculate once
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView.Columns.Count; i++)
{
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font ;
if (tmpFont == null) // If there is no special HeaderFont style, then use the default DataGridView font style
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
tmpWidth = 220; ////tmpWidth = tmpSize.Width;
RowHeaderHeight = 40;////RowHeaderHeight = tmpSize.Height;
for (int j = 0; j < TheDataGridView.Rows.Count; j++)
{
tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
if (tmpFont == null) // If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString("Anything", tmpFont);
RowsHeight.Add(tmpSize.Height);
tmpSize = g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
if (tmpSize.Width > tmpWidth)
tmpWidth = tmpSize.Width;
}
if (TheDataGridView.Columns[i].Visible)
TheDataGridViewWidth += tmpWidth;
ColumnsWidth.Add(tmpWidth);
}
// Define the start/stop column points based on the page width and the DataGridView Width
// We will use this to determine the columns which are drawn on each page and how wrapping will be handled
// By default, the wrapping will occurr such that the maximum number of columns for a page will be determine
int k;
int mStartPoint = 0;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
if (TheDataGridView.Columns[k].Visible)
{
mStartPoint = k;
break;
}
int mEndPoint = TheDataGridView.Columns.Count;
for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
if (TheDataGridView.Columns[k].Visible)
{
mEndPoint = k + 1;
break;
}
float mTempWidth = TheDataGridViewWidth;
float mTempPrintArea = (float)PageWidth - (float)LeftMargin - (float)RightMargin;
// We only care about handling where the total datagridview width is bigger then the print area
if (TheDataGridViewWidth > mTempPrintArea)
{
mTempWidth = 0.0F;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
{
if (TheDataGridView.Columns[k].Visible)
{
mTempWidth += ColumnsWidth[k];
// If the width is bigger than the page area, then define a new column print range
if (mTempWidth > mTempPrintArea)
{
mTempWidth -= ColumnsWidth[k];
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mStartPoint = k;
mTempWidth = ColumnsWidth[k];
}
}
// Our end point is actually one index above the current index
mEndPoint = k + 1;
}
}
// Add the last set of columns
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mColumnPoint = 0;
}
}
// The funtion that print the title, page number, and the header row
private void DrawHeader(Graphics g)
{
CurrentY = (float)TopMargin;
CurrentZ = (float) TopMargin;
// Printing the page number (if isWithPaging is set to true)
if (IsWithPaging)
{
PageNumber++;
string PageString = "شركت سهامي .......
StringFormat PageStringFormat = new StringFormat();
PageStringFormat.Trimming = StringTrimming.Word;
PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
PageStringFormat.Alignment = StringAlignment.Center;
Font PageStringFont = new Font("Zar Mazar", 20, FontStyle.Bold, GraphicsUnit.Point);
RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, PageStringFont).Height);
g.DrawString(PageString, PageStringFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);
CurrentY += g.MeasureString(PageString, PageStringFont).Height;
}
/////////////////////////////////////////////
if (IsWithz)
{
string ZPageString ="\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n: صاحب جمع اموال : امور اداري ناظر: تحويل گيرنده ";
StringFormat ZStringFormat = new StringFormat();
ZStringFormat.Trimming = StringTrimming.Word;
ZStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
ZStringFormat.Alignment = StringAlignment.Far;
Font ZPageStringFont = new Font("Zar Mazar", 14, FontStyle.Bold, GraphicsUnit.Point);
RectangleF ZPageSringRectangle = new RectangleF((float)LeftMargin, CurrentZ, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(ZPageString,ZPageStringFont).Heigh t);
g.DrawString(ZPageString, ZPageStringFont, new SolidBrush(Color.Black), ZPageSringRectangle, ZStringFormat);
CurrentZ -= g.MeasureString(ZPageString, ZPageStringFont).Height;
}
////////////////////////////////////////////////
// Printing the title (if IsWithTitle is set to true)
if (IsWithTitle)
حالا سوالي كه داشتم اينه چطور ميتوانم مقدار pagestring را از textbox1.text فرم 1 بگيرم
(نتيجه جستجوهاي بنده استفاده از متغيير سراسري بين چند فرم بود)
--------------------------------------------------------------------------------------
هيچ جايي مثله برنامه نويسي نميشه