GridView索引超出范围。 Asp.net C# [英] GridView Index was out of range. Asp.net C#

查看:140
本文介绍了GridView索引超出范围。 Asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在使用asp.net c#,我想检查gridview单元格值是否等于textbox值我正在编写此代码,因为它给出了错误..

Hello,
I am using asp.net c# , i want to check that gridview cell value is equal to textbox value i am writing this code by it is giving error ..
"

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








"

if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)
        {
            Loaddata();
        }
        else
        {
            lblerror.text = "Error";
            textbox2.Focus();
        }





请帮助.............



Please Help.............

推荐答案

if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)
        {//Why are you checking cell 5 text with textbox ???
            Loaddata();
   //Note: After this line gridview will populate. If you are loading gridview here
        }
        else
        {
            lblerror.text = "Error";
            textbox2.Focus();
        }





请在前面看到你正在检查gridview行索引0.如果没有绑定到gridview的行,那么它是之前。那么这个如果(Gridview1.Rows [0] .Cells [5] .Text.ToString()== textbox1.Text)将如何工作。



你的条件是错误的,这就是为什么它给你错误



See at the earlier point you are checking gridview row index 0. Where there is no row binded to gridview it's before. So how could this "if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)" will work.

YOur condition is wrong that's why it's giving error to you


就像你的消息说的那样,你试图访问一个外面的数组是范围。错误可能由两个原因产生:

1.您的网格视图只有5列(索引从0到4),并且您正在尝试访问不存在的第6列。 />
2.您正在尝试访问第1行的网格视图,但您的网格视图当时不包含任何行。



PS:您可以在我的下一篇文章中找到有关使用ASPX网格视图的详细信息和示例:高级ASPX GridView分页和数据实体 [ ^ ]
Like you message is saying, you are trying to access an array outside is range. The error could be generated by two reasons:
1.Your grid view has only 5 columns (with indexes from 0 to 4) and you are trying to access the 6th column that does not exist.
2.You are trying to access the grid view 1st row, but you grid view does not contain any row at that time.

PS: You could find details and examples about the using of the ASPX grid view in my next article: Advanced ASPX GridView Pagination and Data Entities[^]


检查gridview中有多少列。 index从0开始,如果你的gridview中有六列,那么它就不会抛出错误。
check how many columns are there in gridview. index start from 0 onwards, if there are six columns in your gridview, then it won't thrown an error.


这篇关于GridView索引超出范围。 Asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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