Can't add rows to DataGridView after adding th

2019-08-22 06:15发布

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

5条回答
\"骚年 ilove
2楼-- · 2019-08-22 06:33

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];
查看更多
我只想做你的唯一
3楼-- · 2019-08-22 06:37

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

查看更多
SAY GOODBYE
4楼-- · 2019-08-22 06:43

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.

查看更多
乱世女痞
5楼-- · 2019-08-22 06:50

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.

查看更多
别忘想泡老子
6楼-- · 2019-08-22 06:51

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.

查看更多
登录 后发表回答