【问题标题】:Can't add rows to DataGridView after adding the first one添加第一个后无法将行添加到 DataGridView
【发布时间】:2011-10-09 14:03:39
【问题描述】:

我有这个代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
       if (e.KeyChar == 13) {
           db database = new db();
           database.set_connection(Properties.Settings.Default.connection);
           DataTable result = database.search(textBox1.Text, "", "");
           if (result.Rows.Count == 0)
           {
               MessageBox.Show("nothing found", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
               textBox1.SelectAll();
               textBox1.Focus();
           }
           else {
               if (dataGridView1.Rows.Count == 0)
               {
                   DataGridViewRow row = new DataGridViewRow();
                   dataGridView1.Rows.Add(row);
                   row.Cells[0].Value = result.Rows[0]["title"].ToString();
                   row.Cells[1].Value = result.Rows[0]["code"].ToString();
                   row.Cells[2].Value = result.Rows[0]["sell"].ToString();
                   row.Cells[3].Value = "1";
                   row.Cells[4].Value = "";
                   decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
                   row.Cells[5].Value = total;

               }
               else {
                   bool found = false;
                   int position = 0;
                   foreach (DataGridViewRow ro in dataGridView1.Rows)
                   {
                       if (ro.Cells[0].Value.ToString() == result.Rows[0]["title"].ToString())
                       {
                           found = true;
                           position = ro.Index;
                           break;
                       }
                   }

                   if (found)
                   {
                       dataGridView1.Rows[position].Cells[3].Value = Convert.ToInt32(dataGridView1.Rows[position].Cells[3].Value) + 1;
                       decimal total = Convert.ToDecimal(dataGridView1.Rows[position].Cells[2].Value) * Convert.ToDecimal(dataGridView1.Rows[position].Cells[3].Value);
                       dataGridView1.Rows[position].Cells[5].Value = total;
                   }
                   else {
                       DataGridViewRow row = new DataGridViewRow();
                       dataGridView1.Rows.Add(row);
                       row.Cells[0].Value = result.Rows[0]["title"].ToString();
                       row.Cells[1].Value = result.Rows[0]["code"].ToString();
                       row.Cells[2].Value = result.Rows[0]["sell"].ToString();
                       row.Cells[3].Value = "1";
                       row.Cells[4].Value = "";
                       decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
                       row.Cells[5].Value = total;
                   }



               }



           }
       }
    }

当我运行我的应用程序时,我可以毫无问题地添加第一行,但是当我想添加第二行时,我收到了这个错误:

Specified argument was out of the range of valid values.
Parameter name: rowIndex

有什么问题??

编辑:

错误发生在:

+       row.Cells[0].Value  'row.Cells[0].Value' threw an exception of type 'System.ArgumentOutOfRangeException'    object {System.ArgumentOutOfRangeException}

编辑 2:

这是我重写某些部分后的全新代码,但我仍然遇到同样的问题

【问题讨论】:

  • 你到底是哪一行超出了数组的边界?如果您通过 Visual Studio 运行程序,它将在问题点中断。

标签: c# datagridview rows


【解决方案1】:

您在迭代集合时正在修改它.. 坏主意。不要在循环中将行添加到DataGridView,只需创建一个列表并将每一行存储在列表中,然后在退出循环后将它们添加到DataGridView

另外,无论result 是什么,在尝试对其内容编制索引之前,您都需要确保它不为空。

【讨论】:

  • 我循环抛出行只是为了检查重复行,以便我可以更新数量字段
【解决方案2】:

在调试器中运行并在Debug -> Exceptions -> CLR Exceptions 中启用异常,看看它会在哪里崩溃。

我只能假设result.Rows[0], 中的一个问题你还没有检查Rows != null and Rows.Count() > 0 是否存在

【讨论】:

    【解决方案3】:

    您的 else 子句中有几个 result.row[0] 实例,我看不出您在哪里声明“结果”,所以如果不是错误的原因,这可能是来源。

    【讨论】:

      【解决方案4】:

      在遍历行的同时向表中添加行似乎是一种糟糕的方法。也许这就是问题所在。

      【讨论】:

        【解决方案5】:

        您只需在每个循环中将当前行设置为最后添加的行和当前单元格 像

        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
        

        然后设置当前单元格 像这样的

        dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多