打印DataGridView的问题

Greg_han 2010-04-19 12:20:38
我写了一个打印DataGridView的函数,在打印预览下,能看到所有数据(列名和具体数据),但是我没有安装打印机,所有只能导入预览界面自带的导入*.tif文件。打开保存的tif文件,发现只有列名,而没有数据。不知道是那里错了?请大家看看我的代码:

//我写的函数:先从数据库中度入数据,再画在printdocument1上
private void printdocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
sqlLanguage = "select FundNum as '基金编号',FundName as '基金名称', LastPrice as '最近净值',CurPrice as '目前净值', cast( (LastPrice-CurPrice)/LastPrice as decimal(10,4) ) as '增长率', BuyDiscountRate as '申购费率', SaleDiscountRate as '赎回费率', FundComp as '基金公司',FundState as '状态'from T_Fund";

SqlDataAdapter adap = new SqlDataAdapter(sqlLanguage, sqlconn);

DataSet ds = new DataSet();

adap.Fill(ds);


e.Graphics.DrawString("基金编号", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 20, 70);
e.Graphics.DrawString("基金名称", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 100, 70);
e.Graphics.DrawString("最近净值", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 180, 70);
e.Graphics.DrawString("目前净值", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 260, 70);
e.Graphics.DrawString("增长率", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 340, 70);
e.Graphics.DrawString("申购费率", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 410, 70);
e.Graphics.DrawString("赎回费率", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 500, 70);
e.Graphics.DrawString("基金公司", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 580, 70);
e.Graphics.DrawString("交易状态", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 660, 70);

int rows = ds.Tables[0].Rows.Count - 1; //获得数据表的行数

while (rows > index)
{
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[0].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 40, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[1].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 100, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[2].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 180, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[3].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 265, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[4].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 340, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[5].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 420, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[6].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 500, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[7].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 565, x + 70);
e.Graphics.DrawString(ds.Tables[0].Rows[index].ItemArray[8].ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 675, x + 70);

x = x + 30;
++index;
if (index % 25 == 0)
{
e.HasMorePages = true;//翻页并继续执行
x = 30;
return;
}
// return;
else
{
e.HasMorePages = false;
}


}
}
...全文
34 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shang123guan 2010-04-20
  • 打赏
  • 举报
回复
给个邮箱,发个打印datagridview的类给你,你只要填充datagridview然后调用类打印就行了
wedy_wang 2010-04-19
  • 打赏
  • 举报
回复
你的冗余信息太多,大家懒的看。你将代码行数减到最少,比如只有一个列,一个行,看能不能出来?
另外,你构造的所有Font对象都没有Dispose,你的Leader肯定会找你的。
Greg_han 2010-04-19
  • 打赏
  • 举报
回复
???
没有人知道吗?

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧