添加第一个行后无法将行添加到 DataGridView

发布于 2024-11-25 10:20:38 字数 3309 浏览 1 评论 0原文

我有这样的代码:

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:

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

I have this code:

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

What is the problem??

EDIT:

the error is happening in:

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

EDIT 2:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Smile简单爱 2024-12-02 10:20:38

您在迭代集合时正在修改它..坏主意。无需在循环中将行添加到 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.

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

小…楫夜泊 2024-12-02 10:20:38

在调试器中运行并在 Debug -> 中启用异常例外情况-> CLR 异常 并查看哪里会崩溃。

我只能假设 result.Rows[0] 中存在问题, 您尚未检查 Rows != null 和 Rows.Count() > 是否为0

Run in debugger and enable exceptions in Debug -> Exceptions -> CLR Exceptions and see where it will crash.

I just can assume that a problem in result.Rows[0], you haven't checked whether Rows != null and Rows.Count() > 0

热情消退 2024-12-02 10:20:38

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

You have several instances of result.row[0] in your else clause, i dont see where you are declaring 'result' so this is probably the source if not the cause of the error.

命硬 2024-12-02 10:20:38

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

Adding rows to a table while looping through the rows seems like a poor way to go about it. Perhaps that is causing the problem.

信愁 2024-12-02 10:20:38

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

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

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

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

You simply need to set the current row to the last added row and currentcell on every loop
something like

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

then set the currentcell
something like this

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文