引用datagridview1中的单元格值,总是显示索引超出范围,必须为非负值并且小于集合大小。

privatevoidprintDocument1_PrintPage(objectsender,System.Drawing.Printing.PrintPageEve... private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Brush bru = Brushes.Black; int g = dataGridView1.ColumnCount;//获得行数; int h = (g * 5 + 5) / 10;//获得页数; for (int i = 1; i <= h; i++) { e.Graphics.DrawString("NANTONG", new Font(new FontFamily("黑体"), 18), bru, 100, 0); e.Graphics.DrawString(txt_kh.Text, new Font(new FontFamily("黑体"), 18), bru, 60, 35); e.Graphics.DrawString(txt_xh.Text, new Font(new FontFamily("黑体"), 18), bru, 60, 70); e.Graphics.DrawString(dataGridView1.Rows[2 * i - 1].Cells[2].Value.ToString(), new Font(new FontFamily("黑体"), 18), bru, 60, 105); e.Graphics.DrawString(txt_xxh.Text, new Font(new FontFamily("黑体"), 12), bru, 180, 105); e.Graphics.DrawString(txt_gh1.Text + " " + txt_gh2.Text + " " + txt_gh3.Text, new Font(new FontFamily("黑体"), 12), bru, 60, 140); e.Graphics.DrawString(txt_rq.Text, new Font(new FontFamily("黑体"), 12), bru, 200, 140); e.Graphics.DrawString(txt_bz.Text, new Font(new FontFamily("黑体"), 18), bru, 60, 175); if (2 * i - 1 < g) { e.Graphics.DrawString("NANTONG", new Font(new FontFamily("黑体"), 18), bru, 455, 0); e.Graphics.DrawString(txt_kh.Text, new Font(new FontFamily("黑体"), 18), bru, 415, 35); e.Graphics.DrawString(txt_xh.Text, new Font(new FontFamily("黑体"), 18), bru, 415, 70); e.Graphics.DrawString(dataGridView1.Rows[2 * i].Cells[2].Value.ToString(), new Font(new FontFamily("黑体"), 18), bru, 415, 105); e.Graphics.DrawString(txt_xxh.Text, new Font(new FontFamily("黑体"), 12), bru, 535, 105); e.Graphics.DrawString(txt_gh1.Text + " " + txt_gh2.Text + " " + txt_gh3.Text, new Font(new FontFamily("黑体"), 12), bru, 415, 140); e.Graphics.DrawString(txt_rq.Text, new Font(new FontFamily("黑体"), 12), bru, 555, 140); e.Graphics.DrawString(txt_bz.Text, new Font(new FontFamily("黑体"), 18), bru, 415, 175); if (2 * i < g) { e.HasMorePages = true; } else { e.HasMorePages = false; } } } } 展开
 我来答
囚笼山人六
2016-08-12 · TA获得超过8564个赞
知道大有可为答主
回答量:8577
采纳率:70%
帮助的人:2946万
展开全部
你必须确认,你的2*i-1是大于0的,要不就会溢出,因为根本没有这个序号
更多追问追答
追问
看见上面for (int i = 1; i <= h; i++)了吗  难道2*i-1不大于0吗
追答
看不到,被你挡住了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式