打印DataGridView的问题
我写了一个打印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;
}
}
}