PDA

View Full Version : مشکل در کلاس پرینت از datagridview



mosavi8002
دوشنبه 25 اسفند 1393, 21:52 عصر
سلام دوستان
میخوام از دیتا گرید ویو پرینت بگیرم .از کلاسش استفاده میکنم
برای این خط ایراد میگیره به نظرتون چرا؟


Image imgPerson = Image.FromFile( Application.StartupPath+ DataGridViewPrinter.imageadress.adress);



بعد برای ارور اینو نشون میده تو اون قسمت


{"C:\\Users\\Ahd\\documents\\visual studio 2012\\Projects\\H-aati\\H-aati\\bin\\Debug":null}

drsina
سه شنبه 26 اسفند 1393, 11:26 صبح
اول که داری لود می کنی ، اینجا
بعدشم Application.Startup یه رشته برات بر می گردونه که آخرش بک اسلش نداره ، یعنی به کدت یه چیزی توو این مایه ها باید بشه :
Application.StartupPath+"\\"

ولی اگه کلان میخوای path به هم متصل کنی از :
System.IO.Path.Combine("path", "path");استفاده کن.

اگرم بازم موفق نشدی ، من قبلان از این کلاس استفاده می کردم ، یه نگاهی بش بنداز »

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




public class Print
{


int iCellHeight = 0;
int iTotalWidth = 0;
int iRow = 0;
bool bFirstPage = false;
bool bNewPage = false;
int iHeaderHeight = 0;
StringFormat strFormat;
ArrayList arrColumnLefts = new ArrayList();
ArrayList arrColumnWidths = new ArrayList();
private PrintDocument _printDocument = new PrintDocument();


DataGridViewRowCollection R = null;
DataGridViewColumnCollection C = null;
Font F = null;


public Print(DataGridViewRowCollection r , DataGridViewColumnCollection c ,Font f )
{
R = r;
C = c;
F = f;
_printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);
_printDocument.BeginPrint += new PrintEventHandler(_printDocument_BeginPrint);
}


public void PrintForm()
{


PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
objPPdialog.Document = _printDocument;
objPPdialog.ShowDialog();
}


private void _printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{


int iLeftMargin = e.MarginBounds.Left;
int iTopMargin = e.MarginBounds.Top;
bool bMorePagesToPrint = false;
int iTmpWidth = 0;


if (bFirstPage)
{
foreach (DataGridViewColumn GridCol in C )
{
iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
(double)iTotalWidth * (double)iTotalWidth *
((double)e.MarginBounds.Width / (double)iTotalWidth))));


iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;


arrColumnLefts.Add(iLeftMargin);
arrColumnWidths.Add(iTmpWidth);
iLeftMargin += iTmpWidth;
}
}
while (iRow <= R.Count - 1)
{
DataGridViewRow GridRow = R[iRow];
iCellHeight = GridRow.Height + 5;
int iCount = 0;
if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
bNewPage = true;
bFirstPage = false;
bMorePagesToPrint = true;
break;
}
else
{


if (bNewPage)
{
e.Graphics.DrawString("",
new Font(F, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left,
e.MarginBounds.Top - e.Graphics.MeasureString("",
new Font(F, FontStyle.Bold),
e.MarginBounds.Width).Height - 13);


String strDate = "";
e.Graphics.DrawString(strDate,
new Font(F, FontStyle.Bold), Brushes.Black,
e.MarginBounds.Left +
(e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
new Font(F, FontStyle.Bold),
e.MarginBounds.Width).Width),
e.MarginBounds.Top - e.Graphics.MeasureString("",
new Font(new Font(F, FontStyle.Bold),
FontStyle.Bold), e.MarginBounds.Width).Height - 13);
iTopMargin = e.MarginBounds.Top;
DataGridViewColumn[] _GridCol = new DataGridViewColumn[C.Count];
int colcount = 0;
foreach (DataGridViewColumn GridCol in C)
{
_GridCol[colcount++] = GridCol;
}
for (int i = (_GridCol.Count() - 1); i >= 0; i--)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));


e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));


e.Graphics.DrawString(_GridCol[i].HeaderText,
_GridCol[i].InheritedStyle.Font,
new SolidBrush(_GridCol[i].InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
iCount++;
}
bNewPage = false;
iTopMargin += iHeaderHeight;
}
iCount = 0;
DataGridViewCell[] _GridCell = new DataGridViewCell[GridRow.Cells.Count];
int cellcount = 0;
foreach (DataGridViewCell Cel in GridRow.Cells)
{
_GridCell[cellcount++] = Cel;
}
for (int i = (_GridCell.Count() - 1); i >= 0; i--)
{
if (_GridCell[i].Value != null)
{
e.Graphics.DrawString(_GridCell[i].FormattedValue.ToString(),
_GridCell[i].InheritedStyle.Font,
new SolidBrush(_GridCell[i].InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount],
(float)iTopMargin,
(int)arrColumnWidths[iCount], (float)iCellHeight),
strFormat);
}
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iCellHeight));
iCount++;
}
}
iRow++;
iTopMargin += iCellHeight;
}
if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false;
}


private void _printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
strFormat.Trimming = StringTrimming.EllipsisCharacter;


arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0;
iRow = 0;
bFirstPage = true;
bNewPage = true;


iTotalWidth = 0;
foreach (DataGridViewColumn dgvGridCol in C)
{
iTotalWidth += dgvGridCol.Width;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


}







حالا این کلاس رو به پروژت ادد کن ، بعد از رووش یه آبجکت بساز و پارامتر هاتو بهش بده و بعد نشون میده ..
موفق باشی :لبخند: