Click here to Skip to main content
15,662,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DGV that houses items to be place on a order. The items were entered by the user
and are not bound to any source table. I can add/change items rows in the DGV. I can delete rows
and the DGV adjusts itself accordingly.
Example: I have seven rows in the DGV the rows are numbered 1-7
I delete row 2
The row disappears. The rows are now numbered 1-6
If I attempt to delete what is now row 2 it gets an exception error
stating I can not access a deleted row.
Any ideas on how to best resolve this problem.
bowlturner 10-Jan-14 11:31am    
My first question would be are the 'row id's being renumbered? or just the visual numbers?
Can you show the code which you trying to delete the row?
Have you rebounded the DGV after deleting the row?
Kees van Spelde 10-Jan-14 14:03pm    
He said the DGV is not bound to any datasource so that is probably not the problem.
For the rest I agree with you... show use some code.
Yes, correct. Thanks :)
ZurdoDev 10-Jan-14 15:39pm    
Yes, you need to keep track of your rows properly.

1 solution

Hello ,

I have created a small application it contains a form with the following controls:

1)2 buttons:
-Add: Adds the data in datagridview.
-Delete: Delete data from the datagridview.


3)2 textboxes:
- textbox1: enter data into the datagridview through this textbox.
- textbox2: this specifies the index at which you want to remove the data. So enter the index at which you want to remove the data.

public partial class Form1 : Form
      public Form1()
          dataGridView1.Columns.Add("col1", "data");////Initialize the columns and give it a name.
          dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(removed);///When you remove an item this event handler fires


      private void add_Click(object sender, EventArgs e)
          dataGridView1.Rows.Add(textBox1.Text);///fetch an item from textbox and add it to the datagridview

      private void delete_Click(object sender, EventArgs e)
          dataGridView1.Rows.RemoveAt(int.Parse(textBox2.Text));////Remove At the location specified in the textbox

      public void removed(object sender, DataGridViewRowsRemovedEventArgs e)

          MessageBox.Show("Removed  ");///pops up when you delete an item


- Please remember suppose you have 3 elements in the datagridview. To delete the 3 element you will have to specify the index : 2, because it is a zero based index, starts from 0 not 1. So the exception you might have got is :
Uncommitted new row cannot be deleted.
(correct me if i am wrong).

- Just try the above code. Hope it helps. This code adds and deletes an item on button press. Please do let me know what happens.

thanks again,
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900