Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
DataGridView - how to delete Row
Posted on: 25 Nov 2007

Hi all,

I am working on C# project using C# 2.0. I have a DataGrid that contains 10 rows and at runtime I insert one more row at DataGridView.Insert(0,1); in my next step, I want to delete the row from DataGridView at DataGridView.RemoveAt(9) // Index 9 is the last row in the grid. But, I am not able to delete the row. I am getting following exception: System.InvalidOperationException:Uncommitted new row cannot be deleted. at System.Windows.Forms.DataGridViewRowCollection.RemoveAt(Int32 index)

Please help if you know the solution.

Thanks in advance for your help.


A.Asif

AuthorReply
Alan
  • 0
  • 0
Re: DataGridView - how to delete Row
Posted on: 25 Nov 2007  

Well, the exception sounds as though you're trying to delete the row used for adding new data at the bottom of the grid. However, that makes no sense, because if you started with 10 rows (including the 'new' row) and then inserted another row at index 0 at runtime, you'll have 11 rows and the 10th (with an index of 9) will be the last one with any data in it. It should therefore be deletable.

Possibly, therefore, you didn't in fact have 10 rows to start with or the Rows collection is not getting updated for some reason.

You can always check whether a row is a 'new' row or not before attempting to delete it, by using the DataGridViewRow.IsNewRow property.

You could therefore try a different approach:

if (dataGridView1.Rows[9].IsNewRow)
   dataGridview1.Rows.RemoveAt[8];
else
   dataGridview1.Rows.RemoveAt[9];


Re: DataGridView - how to delete Row
Posted on: 08 Jan 2008  
Not sure if it helps but search:


http://www.sourcepop.com/GetResults.aspx?q=delete%20row%20datagridview

There are a whole bunch of tutorial links there for the datagrid, add, edit, delete etc. You can compare your code to this and should find some answers I think.

http://www.sourcepop.com

Find code faster.
Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.