【问题标题】:Getting error while accessing cells of row in data grid view在数据网格视图中访问行单元格时出错
【发布时间】:2016-04-23 17:54:09
【问题描述】:

在这里,我在数据网格视图中添加了组合框和复选框。但是当我访问复选框或组合框时,会出现如下错误;

索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

填充数据后我的数据网格视图如下所示,

enter image description here

返回代码是,

private void button3_Click(object sender, EventArgs e)
 {
      DataGridViewRow roow=new DataGridViewRow();;
      int rcnt = dataGridView1.Rows.Count-1;
      int ccnt = dataGridView1.Rows[0].Cells.Count-1;
      for (int i = 1; i <= rcnt; i++)
      {
           DataGridViewCheckBoxCell ischecked= roow.Cells[ccnt] as DataGridViewCheckBoxCell;  
           if((bool)ischecked.Value==true)
           {
               for (int j = 1; j < ccnt; j++)
               {
                    dataGridView1.Rows[rcnt].Cells[ccnt].ReadOnly = true;
               }
           }
           ccnt--;
      }          
}

【问题讨论】:

  • 我不明白为什么你想访问数据网格行,而你只是创建一个新的 DataGridViewRow 并且你没有为它分配列,也没有将它分配给你的数据网格。

标签: c# datagridview


【解决方案1】:

你好,你必须在你的循环中用 0 而不是 1 开始你的 conter "i" 和 "j"

【讨论】:

  • 对不起,它不起作用。索引错误超出范围。必须是非负数且小于集合的大小。参数名称:代码索引,DataGridViewCheckBoxCell ischecked= roow.Cells[ccnt] as DataGridViewCheckBoxCell;
【解决方案2】:

试试这个代码:

 private void button1_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();

            DataColumn col1 = new DataColumn("Name");
            DataColumn col2 = new DataColumn("Username");
            DataColumn col3 = new DataColumn("tr");
            col3.DataType = System.Type.GetType("System.Boolean");


            col1.DataType = System.Type.GetType("System.String");
            col2.DataType = System.Type.GetType("System.String");

            table.Columns.Add(col1);
            table.Columns.Add(col2);
            table.Columns.Add(col3);


            DataRow row = table.NewRow();
            row[col1] = "John";
            row[col2] = "John123";
            row[col3] = true ;

            table.Rows.Add(row);


             row = table.NewRow();
            row[col1] = "John1111";
            row[col2] = "John1225";
            row[col3] = false ;

            table.Rows.Add(row);

            row = table.NewRow();
            row[col1] = "John222";
            row[col2] = "John3333";
            row[col3] = true ;

            table.Rows.Add(row);


            row = table.NewRow();
            row[col1] = "John3333";
            row[col2] = "Jogggazg";
            row[col3] = false ;

            table.Rows.Add(row);


            dataGridView1.DataSource = table;
        }

 private void button2_Click(object sender, EventArgs e)
        {
            DataGridViewRow roow = new DataGridViewRow(); ;
            int rcnt = dataGridView1.Rows.Count - 2;
            int ccnt = dataGridView1.Rows[0].Cells.Count - 1;
            for (int i = 0; i <= rcnt; i++)
            {
                roow = dataGridView1.Rows[i];
                DataGridViewCheckBoxCell ischecked = roow.Cells[ccnt] as DataGridViewCheckBoxCell;
                if ( (bool)ischecked.Value == true)
                {
                    for (int j = 0; j <= ccnt; j++)
                    {
                        dataGridView1.Rows[rcnt].Cells[j].ReadOnly = true;
                    }
                }

            }          
        }

【讨论】:

  • 我收到类似 Object reference not set to an instance of an object. 之类的错误。 if((bool)ischecked.Value==true)
  • 我为你创建了一个项目,我已经编辑了我的帖子如何包含所有代码,他为我工作,再试一次,如果他对你有用,请检查问题是否已解决
猜你喜欢
  • 2011-11-30
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多