How to programmatically add a new row to a DataGridView?


D

Daniel

In a DataGridView with several columns, I have one or more readonly columns.
They show a value of an object that's 'behind' it (Think of an order
object/row that has a property called Product and a readonly propery
ProductName that gets the name of this product. In the DataGridView only
ProductName is used). I implemented a KeyDown event handler and when the
user presses F2 on that column, I open a window, let the user select the
product from a list and set the appropriate property in the row object. The
DataGridView shows the pruduct number of the selected product.

All this works fine, but the DataGridView should create a new row object,
just as it does when you start typing on a column, so it finaly adds the new
row to the datasource when leaving the new row (by pressing the Down or Tab
key on the last column).

The DataGridView is bound to a BindingSource. Calling BindingSource.AddNew()
throws an InvalidOperationException.

I searched the web but all solutions to a very similar problem point out to
handle the DefaultValueNeeded and CellBeginEdit events and call the SendKeys
method to simulate key strokes. Since my column is readonly, this doesn't
work for me.

Is there another solution for my problem?

Thanks in advance
Daniel
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top