I'm creating a simple desktop app (.NET 4.5) for a group of windows users. I'm looking at using Entity Framework (6). The design was database first.
CREATE TABLE [CommodityCombo](
Recid INT IDENTITY PRIMARY KEY NOT NULL,
Name VARCHAR(100) NOT NULL DEFAULT ''
) ON [PRIMARY]
GO
CREATE TABLE [CommodityComboMember] (
Commodity VARCHAR(20) NOT NULL PRIMARY KEY,
ComboId INT NOT NULL REFERENCES CommodityCombo(Recid)
) ON [PRIMARY]
GO
So that's the SQL for two of the tables. The concept was a pair of master/detail datagridviews. The master would be for adding/removing CommodityCombos and the detail for adding/removing members.
So then in the form load event handler I have ...
try
{
db = new MyEntities();
var combos = (from c in db.CommodityCombos
select c);
//this.dataGridView1.DataSource = combos;
this.dataGridView1.DataSource = db.CommodityCombos.ToList();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
Now I was thinking it would pretty simple if the datagridview had the regular add/remove capability. i.e. you could highlight a row and press [Delete] or add information in the bottom blank row. But this doesn't seem to work. The records come in, but the "natural" add/delete doesn't work.
Now I found this article... DataGridView AllowUserToAddRow property doesn't work
And the accepted answer suggests that the thing to do is to pass the query object.
However, this creates an exception... and there's this article that corresponds... Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported ... which basically says I have to use object.toList().
Admittedly, I noticed that the first article's accepted answer hints at using a binding source, but it's missing some of the detail that goes into presenting the results of the query as a binding source while preserving the add/remove capability and simultaneously not throwing an exception.
Can someone shed more light on this? Or maybe point me in the direction of a better resource? Or should I just can the idea and create [New] and [Delete] buttons to handle it manually?