添加第一个后,无法向DataGridView添加行

时间:2011-07-20 16:59:34

标签: c# datagridview rows

我有这段代码:

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:

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

5 个答案:

答案 0 :(得分:3)

你在迭代它时修改一个集合。想法。不要将行添加到循环中的DataGridView,只需创建一个列表并将每行存储在列表中,并在退出循环后将它们添加到DataGridView

此外,无论result是什么,在尝试索引内容之前,您需要确保它不是空的。

答案 1 :(得分:0)

在调试器中运行并在Debug -> Exceptions -> CLR Exceptions中启用例外,并查看它将崩溃的位置。

我可以假设您result.Rows[0],中的问题未检查Rows != null and Rows.Count() > 0是否

答案 2 :(得分:0)

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

答案 3 :(得分:0)

在循环遍历行时向表中添加行似乎是一种不好的方法。也许这就是问题所在。

答案 4 :(得分:0)

您只需将当前行设置为每个循环上最后添加的行和currentcell

之类的东西
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;

然后设置currentcell 像这样的东西

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