添加第一个行后无法向 DataGridView 添加行 [英] Can't add rows to DataGridView after adding the first one

查看:73
本文介绍了添加第一个行后无法向 DataGridView 添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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;
                   }



               }



           }
       }
    }

当我运行我的应用程序时,我可以毫无问题地添加第一行,但是当我想添加第二行时,出现此错误:

When I run my app I can add the first row without any problems but when I want to add a second row, I got this error :

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:

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

This is my full new code after rewriting some parts but I am still having the same problem

推荐答案

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

You are modifying a collection while you iterate over it..bad idea. Instead of adding the rows to the DataGridView in the loop, just create a list and store each row in the list and add them to the DataGridViewafter you exit the loop.

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

Also, whatever result is, you need to make sure that it is not empty before trying to index it's contents.

这篇关于添加第一个行后无法向 DataGridView 添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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