我有一个datagridview来显示我的条目,我想要像excel表一样打印出来。
有什么办法可以完全打印datagridview,或者你建议将我的数据存储和打印出什么样的控件?
或者如何在C#中使用HTML表来帮助我创建和打印它们?
答案 0 :(得分:1)
Here是一个解决方案。
答案 1 :(得分:1)
在网上有很多解决方案。你可以google它 here
答案 2 :(得分:0)
首先,您必须了解.NET Framework库中的类 PrintDocument ,此类可帮助您打印任何矩形 您的任务是将datagridview中的表转换为矩形(或绘制与datagridview中的表匹配的新矩形,包括绘图字符串(这些字符串是单元格的内容)?)。之后,您可以使用 PrintDocument 类中的方法和更多方法开始打印。
http://www.codeproject.com/KB/printing/printingdatagridview.aspx
您必须首先加入codeproject网络才能下载示例代码。
答案 3 :(得分:0)
// make a function named zpt
int rw=dataGridView1.Rows.Count; //define rw as globly variable in form
public void zpt()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
Font font = new Font("Arial", 10);
PaperSize psz = new PaperSize("Custom", 100, 200);
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psz;
pdoc.DefaultPageSettings.PaperSize.Height = 820;
pdoc.DefaultPageSettings.PaperSize.Width = 700;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult res = pd.ShowDialog();
if (res == DialogResult.OK)
{
PrintPreviewDialog prv = new PrintPreviewDialog();
prv.Document = pdoc;
res = prv.ShowDialog();
if (res == DialogResult.OK)
{
pdoc.Print();
}
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 50;
int startY = 65;
int Offset = 40;
graphics.DrawString("Welcome to Bakery Shop", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
string underLine = "------------------------------------------";
graphics.DrawString(underLine, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
int a = dataGridView1.Rows.Count;
for (int i = 0; i < a; i++)
{
graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
graphics.DrawString("\t"+Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
}
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[rw].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[rw].Cells[1].Value = textBox2.Text;
rw++;
}
private void button2_Click(object sender, EventArgs e)
{
// on print Button which is in your window for code this...
zpt();
}