我如何...如何解决错误“索引超出范围.必须为非负数". [英] How do i...how to solve the error "index was out of range.must be non-negative "

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

问题描述

我的代码是


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

推荐答案

我的回答:

My answer:

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.


例如,如果您的dataGridView1只有一列并且您正在尝试,则可能会发生这种情况.索引第二个,如下面的行
This could happen, for instance, if your dataGridView1 has just one column and you are trying to index the second one, like in the below line
报价:

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));


请参见数组教程(C#) [ ^ ].


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆