I have done a testproject of a N-tier application where I have a Windows Forms Project using a WCF Plain service.
I have a form with a Toolbar and a Grid. In Form_Load call the WCF Service and fetch the data I want to display in the grid.
The service returns the data as an Array of dto objecs. I set the grids datasource to that Array and the data displays fine.
I then want to add a new row to the grid and save it back to the database.
Previously when I worked with ADO.NET Datatables I could use .Displaylayout.Bands(0).AddNew() on the grid.
When I tried this I got an error saying that it isn't allowed for this type of datasource.
How do I do Inserts and deletes when the grid is databound to an Array of dtos?
Hi,
You can't add items to an array. This is nothing to do with the grid, the Array simply doesn't support it. What I would do is create a BindingList<T> and populate it with the contents of your array and then using the BindingList as your grid's DataSource. BindingList supports all of the usual binding operations like adding and deleting.
I'll look into that.
I wish that my WCF Service that was autogenerated by a wizard had methods that used a BindingList instead of an Array.
For now I'll have to find/write examples that does the convert back and forth.
There's nothing to it. The BindingList<T> has a constructor that takes in an IList (which an array implements). So you should be able to just pass in your array. I guess you might have a problem if your array is not strongly typed, but in that case, it's just a matter of casting it or, worst-case, looping through the array.
Going the other way is even easier. Just call the ToArray method on the BindingList.
It just occurred to me that you might have meant that you want the grid to update the array and using a BindingList will disconnect it.
If that's the case, then one other option you might try is to use a BindingSource. I don't know if that will work or allow you to add rows, but it's worth a try. Create a BindingSource and pass in your array, then bind the grid to the BindingSource.