By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

DataGridView new row problem

P: 22

I have .NET 3.5 Windows Forms application which contains several DataGridView controls. Application also contains class Item (represents price list item) and ItemCollection with implemented IEnumerable, IList interfaces.

dataGridView1 has set AllowUserToAddRows property = true;

When I set DataSource:
Expand|Select|Wrap|Line Numbers
  1. dataGridView1.DataSource = oInvoice.Items; //returns ItemCollection..
there is no blank row for new record in dataGridView1.

When I set DataSource:
Expand|Select|Wrap|Line Numbers
  1. dataGridView1.DataSource = oInvoice.GetItems(); //returns DataTable..
works everything fine, there is new blank row at end of dataGridView1 rows.

Any advices?

Thank a lot
Dec 21 '08 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 221
According to MSDN, if the grid is bound to data, the user is only allowed to add rows if both the AllowUserToAddRows property and the datasource's IBindingList.AllowNew property are set to true.

When you set up a DataTable as the datasource, the grid is binding to the DataTable via DefaultView. This means it works because although DataTable itself doesn't implement IBindingList, the DataView object does.

ItemCollection doesn't implement IBindingList, so presumably you won't be able to add rows.

In order to fix it you would probably use a different collection type that does implement IBindingList or create a custom collection type that implemented IBindingList yourself (a lot more work)
Dec 22 '08 #2

Expert 100+
P: 173
If using databinding the easiest built-in object I have found to use is System.ComponentModel.BindingList<T>. This implements the iBindingList interface and can be set up as easily as the ItemCollection.

It does have some limitations with sorting, however, casting can expose this methods.
Dec 22 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.