Allow Users to add rows in Data Grid View

While working on a project that required me to create a Windows Forms Application I was binding a list of order objects to a Data Grid View but I also wanted users to be able to add items as well. Initially I thought this would be easy I simply set the AllowUsersToAddRows property on the Data Grid View to true, however when I ran the application this didn’t do anything.

I spent some time investigating this and found that you have to create BindingSource object and set the AlloNew property to true and then it adds an extra row where users can add data, see full code below.

Code Snippet
  1. BindingSource source = new BindingSource();
  2.                     source.AllowNew = true;
  3.                     source.DataSource = order.OrderLines.Line.ToList();
  4.  
  5.                     DgvOrder_OrderLines.DataSource = source;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: