我在C#库存管理系统中进行项目时遇到问题。 在这里,我必须通过打印机使用PRINT按钮打印一些数据网格视图。我在打印datagridView时遇到问题。 我有下面给出的代码。您能否修改代码以使用打印机进行测试来打印datagridview。
这是我的代码:
private void Print_Click(object sender, EventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1170); // all sizes are converted from mm to inches & then multiplied by 100.
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
//pd.PrinterSettings = PrinterSettings.InstalledPrinters.
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
if (t < 1)
{
ev.Graphics.DrawString(dataGridView1., new Font("Times New Roman", 14, FontStyle.Bold), Brushes.Black, 20, 225);
t++;
if (t < 1)
{
ev.HasMorePages = true;
}
else
{
ev.HasMorePages = false;
}
}
}
答案 0 :(得分:0)
这可能会有所帮助:
我不是那么先进的打印,但我使用了这样的PrintDialog:
private void PrintDoc()
{
PrintDialog printDialog = new PrintDialog(); //make a printDialog object
PrintDocument printDocument = new PrintDocument(); // make a print doc object
printDialog.Document = printDocument; //document for printing is printDocument
printDocument.PrintPage += printDocument_PrintPage; //event handler fire
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print();
}
}
如果这没有帮助,根据你的错误和你的代码,我不确定它所谈论的index
是来自dataGridView还是来自ev.graphics,但我认为你遗失了一些代码?
private void printDocument_PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics graphic = ev.Graphics;
foreach (DataRow row in dataGridView1.Rows)
{
string text = row.ToString() //or whatever you want from the current row
graphic.DrawString(text,new Font("Times New Roman", 14, FontStyle.Bold), Brushes.Black, 20, 225);
}
}
答案 1 :(得分:0)