这是我写的第一个打印程序,虽然很基本,但是我很高兴,拿出来和初学者和新人一起分享吧!我做了个简单的Demo有需要的朋友可以下载:ftp://[email protected]:2124/猫猫要努力学打印系列之网格数据.rarusing System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;namespace 企业资源计划
{
/// <summary>
/// 打印数组
/// </summary>
class PrintArray
{
int t = 0;
int rowIndex = 0
, currentRowIndex = 0;
/// <summary>
/// 打印二维数组的方法
/// </summary>
/// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据</param>
/// <param name="Array2">string类型的二维数组 string[,] Array2</param>
public void PrintArray2(PrintPageEventArgs e, string[,] Array2)
{
float x, y;
SolidBrush s = new SolidBrush(Color.Red);
Font fnt = new Font("宋体", 9F, FontStyle.Regular); y = e.MarginBounds.Top;
x = e.MarginBounds.Left; float[] column = new float[Array2.GetLength(1)];
float x1Row = (float)e.MarginBounds.Left
, yRow = (float)e.MarginBounds.Top
, x2Row = (float)e.MarginBounds.Right; for (int i = 0; i < Array2.GetLength(1); i++)
{
column[i] = 0;
for (int j = rowIndex; j < Array2.GetLength(0); j++)
{
if (column[i] < e.Graphics.MeasureString(Array2[j, i], fnt).Width)
{
column[i] = e.Graphics.MeasureString(Array2[j, i], fnt).Width;
}
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;
if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)
{
currentRowIndex = j;
break;
}
}
yRow = (float)e.MarginBounds.Top;
} rowIndex = currentRowIndex; for (; t < Array2.GetLength(0); t++)
{
for (int j = 0; j < Array2.GetLength(1); j++)
{
e.Graphics.DrawString(Array2[t, j], fnt, s, x + 5, y + 6);
x = x + column[j] + 10;
}
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;
y = y + fnt.GetHeight(e.Graphics) + 10; if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)
{
e.HasMorePages = true;
t++;
break;
}
x = e.MarginBounds.Left;
}
x = e.MarginBounds.Left;
for (int i = 0; i < column.Length; i++)
{
x = x + column[i] + 10;
e.Graphics.DrawLine(new Pen(Color.Red), x, e.MarginBounds.Top, x, yRow);
}
e.Graphics.DrawLine(new Pen(Color.Red), e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Left, yRow);
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);
}
/// <summary>
/// 重置 PrintArray 此方法在打印或打印预览完成时调用
/// </summary>
public void Dispose()
{
t = 0;
rowIndex = 0;
currentRowIndex = 0;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;namespace 企业资源计划
{
/// <summary>
/// 打印数组
/// </summary>
class PrintArray
{
int t = 0;
int rowIndex = 0
, currentRowIndex = 0;
/// <summary>
/// 打印二维数组的方法
/// </summary>
/// <param name="e">为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据</param>
/// <param name="Array2">string类型的二维数组 string[,] Array2</param>
public void PrintArray2(PrintPageEventArgs e, string[,] Array2)
{
float x, y;
SolidBrush s = new SolidBrush(Color.Red);
Font fnt = new Font("宋体", 9F, FontStyle.Regular); y = e.MarginBounds.Top;
x = e.MarginBounds.Left; float[] column = new float[Array2.GetLength(1)];
float x1Row = (float)e.MarginBounds.Left
, yRow = (float)e.MarginBounds.Top
, x2Row = (float)e.MarginBounds.Right; for (int i = 0; i < Array2.GetLength(1); i++)
{
column[i] = 0;
for (int j = rowIndex; j < Array2.GetLength(0); j++)
{
if (column[i] < e.Graphics.MeasureString(Array2[j, i], fnt).Width)
{
column[i] = e.Graphics.MeasureString(Array2[j, i], fnt).Width;
}
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;
if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)
{
currentRowIndex = j;
break;
}
}
yRow = (float)e.MarginBounds.Top;
} rowIndex = currentRowIndex; for (; t < Array2.GetLength(0); t++)
{
for (int j = 0; j < Array2.GetLength(1); j++)
{
e.Graphics.DrawString(Array2[t, j], fnt, s, x + 5, y + 6);
x = x + column[j] + 10;
}
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);
yRow = yRow + fnt.GetHeight(e.Graphics) + 10;
y = y + fnt.GetHeight(e.Graphics) + 10; if (yRow + 10 + fnt.GetHeight(e.Graphics) > e.MarginBounds.Height)
{
e.HasMorePages = true;
t++;
break;
}
x = e.MarginBounds.Left;
}
x = e.MarginBounds.Left;
for (int i = 0; i < column.Length; i++)
{
x = x + column[i] + 10;
e.Graphics.DrawLine(new Pen(Color.Red), x, e.MarginBounds.Top, x, yRow);
}
e.Graphics.DrawLine(new Pen(Color.Red), e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Left, yRow);
e.Graphics.DrawLine(new Pen(Color.Red), x1Row, yRow, x, yRow);
}
/// <summary>
/// 重置 PrintArray 此方法在打印或打印预览完成时调用
/// </summary>
public void Dispose()
{
t = 0;
rowIndex = 0;
currentRowIndex = 0;
}
}
}
如果有一个按钮是“重命名字段”
有一个textbox 用来写你要改的名字
如何在事件中把选中的项的内容改掉?并且让它保持原来的索引位置。用什么方法?
private void resetname_Click(object sender, System.EventArgs e)
{
??????
}