如何删除“值必须为非负值”错误? [英] How do I remove the "value must be non-negative" error?

查看:75
本文介绍了如何删除“值必须为非负值”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我遇到了DataGridView1_CellClick事件的问题。每当我点击任何列(单元格)时,我都会收到错误

Hello,

I am having an issue with DataGridView1_CellClick event. Whenever I am clicking on any column(cell), I am getting an error

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index





错误在第一行本身:



.

The error is in the first line itself:

if (Convert.ToString(DataGridView1.Rows[e.RowIndex].Cells[6].Value) != "")





请帮忙。



问候

Aman Chaurasia



我尝试过:





Please help.

Regards
Aman Chaurasia

What I have tried:

try
            {
                if (Convert.ToString(DataGridView1.Rows[e.RowIndex].Cells[6].Value) != "")
                {
                    if (ZonesItems == null)
                    {
                        if (PictureBox1.Image == null)
                        {
                            DrawZonesFromDgv(Convert.ToString(DataGridView1.Rows[e.RowIndex].Cells[6].Value));
                        }
                        else
                        {
                            DrawZonesFromDgv();
                        }
                        if (ZonesItems.Count > 0)
                        {
                            foreach (var Zone in ZonesItems)
                            {
                                if (Zone.ZoneNo == Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells[6].Value))
                                {
                                    Zone.Clicked = true;
                                }
                                else
                                {
                                    Zone.Clicked = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ZonesItems.Count > 0)
                        {
                            foreach (var Zone in ZonesItems)
                            {
                                if (Zone.ZoneNo == Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells[6].Value))
                                {
                                    Zone.Clicked = true;
                                }
                                else
                                {
                                    Zone.Clicked = false;
                                }
                            }
                        }
                    }
                    PictureBox1.Invalidate();
                }
                if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
                {
                    RichTextBox2.Text = Convert.ToString(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                    DatagridRowClicked = e.RowIndex;
                    DatagridColClicked = e.ColumnIndex;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

推荐答案

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index



使用调试器检查变量的一行错误。

检查 DataGridView1 的行数,并与 e.RowIndex进行比较

检查数量 DataGridView1.Rows [e.RowIndex] 中的单元格并确保至少有7个。



您的代码不要按照你期望的方式行事,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码在做什么,你的任务是与它应该做的事情进行比较。


Use the debugger to inspect variable a line of error.
Check number of rows of DataGridView1 and compare with e.RowIndex
Check number of Cells in DataGridView1.Rows[e.RowIndex] and make sure there is at least 7.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何删除“值必须为非负值”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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