错误:索引超出范围.必须为非负数并且小于集合的大小.参数名称:索引 [英] Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

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

问题描述

大家好,当我按下按钮发布文件时,您能帮我吗?显示此错误

hello everyone can you help me please when I Press button to publish the file this error was show

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


注意:我在GridView1和GridView1内都使用GridView2并具有CommandName,而GridView2也具有CommandName,该错误发生在GridView2中.

定义GridView1索引的代码:


Note:I do GridView2 inside GridView1 and GridView1 have a CommandName and GridView2 also have CommandName, the error happen in GridView2.

the code for define index for GridView1:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            int artid = Convert.ToInt32(((Label)GridView1.Rows[index].FindControl("lblartid")).Text);
            bool pub = ((CheckBox)GridView1.Rows[index].FindControl("chkpub")).Checked;
.
.
.
.



用于定义GridView2索引的代码以及错误所在的位置,我在行下用do对其进行了标记,但是我认为index的定义中存在错误:



the code for define the index for GridView2 and where the error located I mark it by do under line, but I thing the error in the Definition of index :

  protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish1")
        {
            int index = Convert.ToInt32(e.CommandArgument);
           
            GridViewRow row =GridView1.Rows[index];

            GridView grd = new GridView();

            grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));
     

          int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);
           
           bool pub = ((CheckBox)grd.Rows[index].FindControl("chkpub1")).Checked;
.
.
.
.



请快速帮助我



Please help me Quickly

推荐答案

您说的主要内容是确定第二个GridView在最少索引+1行?
如果您主要在GridView2里面有一些行,答案是,但是我想我的问题是,当我在下面的新代码中执行此操作时,如何在GridView1内的GridView2处定义新索引,问题就解决了,但是代码不起作用网页重新加载,但没有区别:

what you main when you say are you sure that this second GridView has at least index+1 rows?
if you main if the GridView2 have some rows inside it the answer yes but I thing my problem is how I can define new index for GridView2 where it is inside GridView1 when I do this the below new code the Problem is finish but the code not work the web page reload but No difference happen:

 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish1")
        {
          int index1 = Convert.ToInt32(e.CommandArgument);

          if (e.CommandName == "publish")
           {
              int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow row = GridView1.Rows[index];

                GridView grd = new GridView();

                grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));

                int flsid = Convert.ToInt32(((Label)grd.Rows[index1].FindControl("lblflsid")).Text);

                bool pub = ((CheckBox)grd.Rows[index1].FindControl("chkpub1")).Checked;



你能帮我吗



can you help me


先生. csharpbd2如何在此代码中尝试使用0替换索引变量

mr. csharpbd2 how I can try use 0 replace index variable in this code

int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);



很抱歉,但我是编程的初学者.



I''m sorry but I''m Beginner in programming


您似乎正在使用index作为两个不同集合的索引: GridView1.Rows[index]行(即grd)中的GridView2的行.您确定第二个GridView至少有index+1行吗?
而且,您正在创建并丢弃grd声明中的GridView实例.将这一行和下一行简化为一行,如下所示:
It looks like you are using index as the index into two different collections: GridView1.Rows and the Rows of the GridView2 that is in the row GridView1.Rows[index] (i.e., grd). Are you sure that this second GridView has at least index+1 rows?
Also, you''re creating and throwing away an instance of GridView in the declaration of grd. Simplify this line and the next into one line, like this:
GridView grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));


这篇关于错误:索引超出范围.必须为非负数并且小于集合的大小.参数名称:索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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