Entity Framework and DataGridViews

Soldato
Joined
27 Feb 2003
Posts
7,218
Location
Shropshire
Is anyone up to speed on this?

I've got a couple of DGVs which are master / detail pulling information using Entity Framework (v5) from a SQL Server.

I'm trying to change one column to be a combobox lookup but I keep getting errors like:

DataGridViewComboBoxCell value is not valid

Code:
        dgvc.Name = "Contract Type"
        dgvc.DataPropertyName = "ContractTypeID"
        dgvc.ValueMember = "tblContractType.ContractTypeID"
        dgvc.DisplayMember = "tblContractType.ContractTypeDescription"

It obviously needs something different from a dataset or similar datasource but I'm stumped on exactly what. I've tried some different strings for ValueMember and DisplayMember but no success.

Cheers,

Chris.
 
Hah, five minutes after posting I sorted it :rolleyes: :D

Code:
Dim qryContractTypes = context.tblContractTypes

Dim dgvc As New DataGridViewComboBoxColumn
dgvc.Name = "Contract Type"
dgvc.DataSource = qryContractTypes.ToList()
dgvc.DataPropertyName = "ContractTypeID"
dgvc.ValueMember = "ContractTypeID"
dgvc.DisplayMember = "ContractTypeDescription"
 
Back again!

Using EF 5 and it seems MS have done half a job with it.

I have a project which has the Entity Framework stuff in and from another project (the GUI), from which I can easily reference and access data eg:

Code:
ctxtCustomers = New ZeusEntities
Dim qryCustomers = ctxtCustomers.tblCustomers
bsCustomers.Datasource = qryCustomers

That pulls the information from the SQL DB and sets the Data Source for Binding Source (bsCustomers) so the information is shown on the DataGridView.

However the query is returned as IQueryable (IIRC) which doesn't let you sort the columns (by clicking the headers) :rolleyes:

I've found some code on the web which will put the query results into a SortableBindlingList.

However, once you use the SortableBindingList as the datasource for the bindingsource, changes made on the datagridview are no longer sent from the bindingsource to the SQL Server. In otherwords, you can't add / update / delete records as ctxtCustomers.SaveChanges() does nothing :mad:

So, help... What's the best way to have sorting and editing (CRUD operations) on a DGV which gets data via EF5?
 
Back again!

Using EF 5 and it seems MS have done half a job with it.

I have a project which has the Entity Framework stuff in and from another project (the GUI), from which I can easily reference and access data eg:

Code:
ctxtCustomers = New ZeusEntities
Dim qryCustomers = ctxtCustomers.tblCustomers
bsCustomers.Datasource = qryCustomers

That pulls the information from the SQL DB and sets the Data Source for Binding Source (bsCustomers) so the information is shown on the DataGridView.

However the query is returned as IQueryable (IIRC) which doesn't let you sort the columns (by clicking the headers) :rolleyes:

I've found some code on the web which will put the query results into a SortableBindlingList.

However, once you use the SortableBindingList as the datasource for the bindingsource, changes made on the datagridview are no longer sent from the bindingsource to the SQL Server. In otherwords, you can't add / update / delete records as ctxtCustomers.SaveChanges() does nothing :mad:

So, help... What's the best way to have sorting and editing (CRUD operations) on a DGV which gets data via EF5?

Not looked at EF 5 yet, but have you thought about moving from VB.NET to C#? You'll find much better support.
 
Back
Top Bottom