请教关于c#桌面程序(也就是c/s程序)调用打印机时报错是什么回事?该怎样解决这问题?谢谢!我先在程序中写了一个打印类,代码如下,再在窗体上添加一dataGrid1(内有数据填充)和一个“打印”按钮,按钮的代码为private void button3_Click(object sender, System.EventArgs e)
{
cutePrinter dgp=new cutePrinter(this.dataGrid1,"病历管理");
dgp.Print(); }可运行程序后点“打印”按钮却报错如下,请问是怎么回事?该怎样解决这问题?谢谢!
索引超出范围。必须为非负值并小于集合大小。
参数名: index有关调用实时(JIT)调试而不是此对话框的详细信息,
请参阅此消息的结尾。************** 异常文本 **************
System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
************** 已加载的程序集 **************
mscorlib
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
yygl
程序集版本: 1.0.2498.36187
Win32 版本: 1.0.2498.36187
基本代码: file:///G:/医院管理系统%20病历管理系统/yygl/bin/Debug/yygl.exe
----------------------------------------
System.Windows.Forms
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Data
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
Accessibility
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------
System.Xml
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
mscorlib.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
----------------------------------------
System.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.resources.dll
----------------------------------------************** JIT 调试 **************
计算机的配置文件(machine.config)的
system.windows.forms 节中必须设置 jitDebugging 值。
编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
打印类代码如下网址所示:
http://www.jzfybjy.com/liou123/sccx/uploadfile/打印类代码.txt
{
cutePrinter dgp=new cutePrinter(this.dataGrid1,"病历管理");
dgp.Print(); }可运行程序后点“打印”按钮却报错如下,请问是怎么回事?该怎样解决这问题?谢谢!
索引超出范围。必须为非负值并小于集合大小。
参数名: index有关调用实时(JIT)调试而不是此对话框的详细信息,
请参阅此消息的结尾。************** 异常文本 **************
System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
************** 已加载的程序集 **************
mscorlib
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
yygl
程序集版本: 1.0.2498.36187
Win32 版本: 1.0.2498.36187
基本代码: file:///G:/医院管理系统%20病历管理系统/yygl/bin/Debug/yygl.exe
----------------------------------------
System.Windows.Forms
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Data
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
Accessibility
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------
System.Xml
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
mscorlib.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
----------------------------------------
System.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.resources.dll
----------------------------------------************** JIT 调试 **************
计算机的配置文件(machine.config)的
system.windows.forms 节中必须设置 jitDebugging 值。
编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
打印类代码如下网址所示:
http://www.jzfybjy.com/liou123/sccx/uploadfile/打印类代码.txt
解决方案 »
- 请问SqlParameter.SourceColumn属性,这里怎么解释?
- WCF技术,会的请进
- 高效率 展开TreeView
- 哪位哥哥姐姐给详细介绍一下C#的异常处理?
- 向数据库中添加数据
- 为什么Excel在服务器端可以打开,而在客户端却不能打开Excel文件呢?(一旦好使,马上给分!!!)
- 水晶报表在98下部署问题,有高分送,急
- 关于公司统一开发(100分)
- 请问在c#里能够编写一个像tcpdump/windump的程序吗?
- VC#中好像没有像ASP。NET中的DATALIST控件吧?请帮忙在C#中我怎样实现像DataList那样一个单元一个单元地显示数据呢?
- 请问那种特别精美的带有flash导航的网页的开发工具是什么?
- 几个面视题 很简单 帮下
------------------>>
这个是VS2003内打印datagrid的!!
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data; using System.Windows.Forms; namespace prjCXSF
{
public class cutePrinter
{
private DataGrid dataGrid;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog; private string title=""; int currentPageIndex=0;
int rowCount=0;
int pageCount=0; int titleSize=20;
bool isCustomHeader=false; Brush alertBrush=new SolidBrush(Color.Red); string[] header=null;//如果自定义就填入字符串,如果需要斜线分隔,就用\表示,例如:个数#名字 其中#为splitChar
string[] uplineHeader=null;//上行文字数组
int[] upLineHeaderIndex=null;//上行的文字index,如果没有上行就设为-1; public bool isEveryPagePrintTitle=true;//是否每一页都要打印标题。
public int headerHeight=30;//标题高度。
public int topMargin=30; //顶边距
public int cellTopMargin=6;//单元格顶边距
public int cellLeftMargin=4;//单元格左边距
public char splitChar='#';//当header要用斜线表示的时候
public string falseStr="×";//如果传进来的dataGrid中有 false,把其转换得字符。
public string trueStr="√";//如果传进来的dataGrid中有 true,把其转换得字符。
public int pageRowCount=35;//每页行数
public int rowGap = 30;//行高
public int colGap = 5;//每列间隔
public int leftMargin = 120;//左边距
public Font titleFont=new Font("黑体",20,FontStyle.Bold);//标题字体
public Font font = new Font("宋体", 10);//正文字体
public Font headerFont = new Font("黑体", 11, FontStyle.Bold);//列名标题
public Font footerFont=new Font("Arial",8);//页脚显示页数的字体
public Font upLineFont=new Font("Arial",9, FontStyle.Bold);//当header分两行显示的时候,上行显示的字体。
public Font underLineFont=new Font("Arial",8);//当header分两行显示的时候,下行显示的字体。
public Brush brush = new SolidBrush(Color.Black);//画刷
public bool isAutoPageRowCount=true;//是否自动计算行数。
public int buttomMargin=80;//底边距
public bool needPrintPageIndex=true;//是否打印页脚页数 //string filterStr=""; public cutePrinter(DataGrid dataGrid,string title,int titleSize)
{
this.title=title;
this.titleSize=titleSize;
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public cutePrinter(DataGrid dataGrid,string title)
{
this.title=title;
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public cutePrinter(DataGrid dataGrid)
{
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
} public bool setTowLineHeader(string[] upLineHeader,int[] upLineHeaderIndex)
{
this.uplineHeader=upLineHeader;
this.upLineHeaderIndex=upLineHeaderIndex;
this.isCustomHeader=true;
return true;
}
public bool setHeader(string[] header)
{
this.header=header;
return true; }
{ int width=e.PageBounds.Width;
int height=e.PageBounds.Height; if(this.isAutoPageRowCount)
pageRowCount=(int)((height-this.topMargin-titleSize-this.headerFont.Height-this.headerHeight-this.buttomMargin)/this.rowGap); pageCount=(int)(rowCount/pageRowCount);
if(rowCount%pageRowCount>0)
pageCount++; int xoffset=(int)((width-e.Graphics.MeasureString(this.title,this.titleFont).Width)/2);
int colCount = 0;
int x = 0;
int y =topMargin;
string cellValue = ""; int startRow=currentPageIndex*pageRowCount;
int endRow=startRow+this.pageRowCount<rowCount?startRow+pageRowCount:rowCount;
int currentPageRowCount=endRow-startRow;
if(this.currentPageIndex==0 || this.isEveryPagePrintTitle)
{
e.Graphics.DrawString(this.title,titleFont,brush,xoffset,y);
y+=titleSize;
} colCount = dataGrid.TableStyles[0].GridColumnStyles.Count; y += rowGap;
x = leftMargin;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);//最左边的竖线 int lastIndex=-1;
int lastLength=0;
int indexj=-1; for(int j = 0; j < colCount; j++)
{
int colWidth=dataGrid.TableStyles[0].GridColumnStyles[j].Width;
if( colWidth> 0)
{
indexj++;
if(this.header==null || this.header[indexj]=="")
cellValue = dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;
else
cellValue=header[indexj]; if(this.isCustomHeader)
{
if(this.upLineHeaderIndex[indexj]!=lastIndex)
{ if(lastLength>0 && lastIndex>-1)//开始画上一个upline
{
string upLineStr=this.uplineHeader[lastIndex];
int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
if(upXOffset<0)
upXOffset=0;
e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2)); DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
}
lastIndex=this.upLineHeaderIndex[indexj];
lastLength=colWidth+colGap;
}
else
{
lastLength+=colWidth+colGap;
}
} //int currentY=y+cellTopMargin;
int Xoffset=10;
int Yoffset=20;
int leftWordIndex=cellValue.IndexOf(this.splitChar);
if(this.upLineHeaderIndex!=null && this.upLineHeaderIndex[indexj]>-1)
{ if(leftWordIndex>0)
{
string leftWord=cellValue.Substring(0,leftWordIndex);
string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
//上面的字
Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
Yoffset=(int)(this.headerHeight/2-e.Graphics.MeasureString("a",this.underLineFont).Height);
Xoffset=6;
Yoffset=10;
e.Graphics.DrawString(rightWord,this.underLineFont,brush,x+Xoffset-4,y+(int)(this.headerHeight/2));
e.Graphics.DrawString(leftWord,this.underLineFont,brush,x+2,y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2)+Yoffset-2);
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
else
{ e.Graphics.DrawString(cellValue, headerFont, brush, x, y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2));
x += colWidth + colGap;
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
} }
else
{
if(leftWordIndex>0)
{
string leftWord=cellValue.Substring(0,leftWordIndex);
string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
//上面的字
Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
Yoffset=(int)(this.headerHeight-e.Graphics.MeasureString("a",this.underLineFont).Height); e.Graphics.DrawString(rightWord,this.headerFont,brush,x+Xoffset-4,y+2);
e.Graphics.DrawString(leftWord,this.headerFont,brush,x+2,y+Yoffset-4);
DrawLine(new Point(x,y),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y+cellTopMargin);
x += colWidth + colGap;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
} } }
}
////循环结束,画最后一个的upLine
if(this.isCustomHeader)
{ if(lastLength>0 && lastIndex>-1)//开始画上一个upline
{
string upLineStr=this.uplineHeader[lastIndex];
int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
if(upXOffset<0)
upXOffset=0;
e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2)); DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
} } int rightBound=x; DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics); //最上面的线 //DrawLine(new Point(leftMargin,y+this.headerHeight),new Point(rightBound,y+this.headerHeight),e.Graphics);//列名的下面的线 y+=this.headerHeight;
//print all rows
for(int i = startRow; i < endRow; i++)
{ x = leftMargin;
for(int j = 0; j < colCount; j++)
{
if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = dataGrid[i,j].ToString();
if(cellValue=="False")
cellValue=falseStr;
if(cellValue=="True")
cellValue=trueStr; e.Graphics.DrawString(cellValue, font, brush, x+this.cellLeftMargin, y+cellTopMargin);
x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] {'\r', '\n'}).Length - 1);
}
}
DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics);
y += rowGap;
}
DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics); currentPageIndex++; if(this.needPrintPageIndex)
e.Graphics.DrawString("共 "+pageCount.ToString()+" 页,当前第 "+this.currentPageIndex.ToString()+" 页",this.footerFont,brush,width-200,(int)(height-this.buttomMargin/2-this.footerFont.Height)); string s = cellValue;
string f3 = cellValue; if(currentPageIndex<pageCount)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
this.currentPageIndex=0; }
//iPageNumber+=1;
}
{
Pen pen=new Pen(Color.Black);
gp.DrawLine(pen,sp,ep);
} public PrintDocument GetPrintDocument()
{
return printDocument;
} public void Print()
{ rowCount=0; if(dataGrid.DataSource.GetType().ToString() == "System.Data.DataSet")
{
rowCount= ((DataSet)dataGrid.DataSource).Tables[0].Rows.Count;
}
else if(dataGrid.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList)dataGrid.DataSource).Count;
}
try
{
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 800; printPreviewDialog.ShowDialog();
}
catch(Exception e)
{
throw new Exception("Printer error." + e.Message);
} }
}
}
------------------------>>
这个是一个打印类呀!!对datagrid的!!private void button1_Click(object sender, System.EventArgs e)
{
cutePrinter dgp=new cutePrinter(this.dataGrid1,"机动车辆驾驶员卡片打印系统");
dgp.Print();
}
________________________>>
if(dataGrid.DataSource.GetType().ToString() == "System.Data.DataSet")
{
rowCount= ((DataSet)dataGrid.DataSource).Tables[0].Rows.Count;
}
else if(dataGrid.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList)dataGrid.DataSource).Count;
} 这个是主要点!!"System.Data.DataSet" 说明dataGrid绑订用的是dataset!!如果用别的要改的!!
请去下载我的 http://community.csdn.net/Expert/topic/5120/5120808.xml?temp=.1856958
请知道的大侠帮忙回一下啊!谢谢!