Can't add rows to DataGridView after adding the first one

南笙酒味 提交于 2019-12-25 12:09:24

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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:


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


来源:https://stackoverflow.com/questions/6765610/cant-add-rows-to-datagridview-after-adding-the-first-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!