shen chang

shen chang

  • NA
  • 2
  • 0

delete last row have exception

Dec 19 2014 5:54 PM
A winform has a datagridview gui, it has 5 columns

//I add 3 rows and the try to delete the last row, got exception:

int k=0;
while (k < 3)
{              
   // add 3 rows
   DataGridViewRow row = new DataGridViewRow();
   row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
   int i= 0;
   //dup the first row
   foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
   {
       row.Cells[i].Value = cell.Value;
       i++;
   }
   dataGridView1.Rows.Add(row); 
   k++;
}

//give them values
 for (int i = 0; i < 3; i++)
{              
        for(int j = 0;j<dataGridView1.ColumnCount;j++)
        {
                dataGridView1[j, i ].Value = j.ToString() + ":" + i.ToString();
        }
}

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
             
//this got exception, but I can delete second row and first row
dataGridView1.Rows.RemoveAt(dataGridView1.RowCount-1);      


Answers (2)