本文介绍了我如何...如何解决错误“索引超出范围.必须为非负数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是


my code is


  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int height = 0;
            int width = 0;
            Pen p = new Pen(Brushes.Black, 2.5f);
            #region Invoice
            e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawRectangle(p, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(25,25, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            #endregion

            #region name
            e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(5 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawRectangle(p, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
            #endregion
        }

What I have tried:

I had tried many values instead of 25

推荐答案

Make the index greater than or equal to 0.



这是一个与问题质量相匹配的答案质量的完美示例.



This is a perfect example of the quality of an answer that matches the quality of the question.


报价:

e.Graphics.DrawString(dataGridView1.Columns [ 1 ].HeaderText.ToString(),dataGridView1.字体,Brushes.Black,新的Rectangle(10 + dataGridView1.Columns [0] .Width,10,dataGridView1.Columns [0] .Width,dataGridView1.Rows [0] .Height));

e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + dataGridView1.Columns[0].Width, 10, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));



这篇关于我如何...如何解决错误“索引超出范围.必须为非负数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:07