Hello, I am trying to bind a WinGrid to my simple Entity Framework CodeFirst data. I created a few simple models, a DBContext, an implementation of a generic Repository Pattern and a UnitOfWork DataaccessLayer to access my database.
But I'm having problems binding the grid to it, as all the datasources somehow do not work and the EntityDataSource does not exist anymore in the latest EF/.NET version. The only way that seems to work, is to surpass the repository and access the DbSet directly:
grid.DataSource = dal.getContext().Students.Local.ToBindingList();
dal is an object of a class containing all the repositories, and getContext returns the DBContext containing all the DbSet's.
It works, but it feels rather....dirty. Usually, the Entity Framework offers such amazing and simple features, but binding to Controls (not only infragistics) seems to be a huge pain. Is there really no way to use visual binding and automatically generated Data/column schemes and all that stuff I had with old ado.net datasets?
Hello Kevin,
I am just checking about the progress of this issue. Let me know if you need my further assistance on it.
Thank you for using Infragistics Components.
Please find some additional information regarding your issue. The reason why cell is not updated is probably because the BindingManager in DotNet doesn’t save a single row because there’s no way to move off that row to another row in the same collection. It might also be because you are using IList as the return type for the child band. The BindingManager doesn’t like it when you use an interface like that instead of a concrete type like List<T>. IList is also not a good type to use for binding anyway, since it doesn’t support all of the notifications the grid needs.
As I mention in my previous post you may use a BindingList<T>. Another thing you may try is to use a BindingSource to wrap his data source and bind the grid to the BindingSource. The BindingSource takes case of a lot of these little hole and oversights.
Thank you for your feedback.
I have checked your code and found you are adding in your Website class ObservableListSource of Aliases. Please note ObservableListSource does not have AddNew() method, while BindingList has it. To fix your issue you can change your aliases collection type in Website class from ObservableListSource to BindingList.
Please find attached a sample solution implementing aliases collection like BindingList instead of ObservableListSource.
Please check my solution and let me know if this is what you are looking for or if I am missing something.
Also I’m going to consult with our developers and will make some more test in order to find out why your approach works in some scenarios.
Thank you for your understanding and patience.
To provide some code:
private void bindGrid(){
dal.dbContext.Websites.Load();
ultraGrid2.DataSource = dal.dbContext.Websites.Local.ToBindingList();
ultraGrid2.DataBind();
}
private void saveButton_Click(object sender, EventArgs e){ this.Validate(); foreach (var alias in dal.dbContext.Aliases.Local.ToList()) {
if (alias.Website == null)
{
dal.dbContext.Aliases.Remove(alias);
dal.dbContext.SaveChanges();
dal.Save();
ultraGrid2.Refresh();
My Website model Looks like
public class Website {
private readonly ObservableListSource<Alias> _aliases = new ObservableListSource<Alias>();
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Name { get; set; }
public string URL { get; set; }
public virtual ObservableListSource<Alias> Aliases { get { return _aliases; } }
public class Alias {
public int WebsiteID { get; set; }
public virtual Website Website { get; set; }
public string AliasName { get; set; }
Hi Milko, thanks a lot for the link. I did try it exactly as in the link, and everything works except that if I want to add a row in the Children data (Band 1), it only inserts them into the database if I add at least 2 rows at the same time. If I add only one child row, it does not save it to the database.
What can that be?