0

Using c#, net.4.5 vs 2012

I'm trying to get data from database using Entity Framework.

At first idea was use code like below - it's must allow to add, delete and update entries in dataGridView, and than just save changes using context.SaveChanges() (method from ObjectSet, if I'm not wrong)

using (LibraryEntities context = new LibraryEntities())
{
            var query = (from c in context.Book select c).First();
            DataGridView dgv = new DataGridView();
            dgv.DataSource = context.Book;
}

Result - exception

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported...

Then I try a little bit changed code:

using (LibraryEntities context = new LibraryEntities())
{
    var query = (from c in context.Book select c).First();
    DataGridView dgv = new DataGridView();
    dgv.DataSource = query;
}

but have same issue with exception

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported...

At last try to convert all to List():

using (LibraryLib.LibraryEntities context = new LibraryLib.LibraryEntities())
{
    DataGridView dgv = new DataGridView();
    dgv.DataSource = (from c in context.Book select c).ToList();
}

As and expected, I got all db entries in DataGridView, but I can't add, update and delete any entries.

enter image description here

And the question is - how can I change db in dataGridView and than save it with DbContext.SaveChanges()

7
  • @ElliotTereschuk, SO is an english only site. Google offer's this as a translation Hey. Try to consider the ready examples are not quite understand everything Commented Jan 13, 2014 at 18:57
  • @paqogomez Thanks, I know - it's because of rare case of seeing Ukrainian people here ;) Commented Jan 13, 2014 at 18:59
  • @Elliot Tereschuk Привіт, а що саме нечітко описано? / HI, what not correctly described in question? Commented Jan 13, 2014 at 19:04
  • @Elliot Tereschuk. так, але коли я приводжу до колекції, я отримую результат. проблема в тому. що я не можу видалити або додати запис до DataGridView В останньому варіанті - все добре працює. Що хочу я - пернести всю базу даних до DataGridView. редагувати її (видаляти міняти значення додавати рядки. тощо). А потім повністю зберегти. Як це зробити?, адже спробувавши так як описано в книжці - видає виключення. Commented Jan 13, 2014 at 19:19
  • @Elliot Tereschuk. Yep, but when i'll try to get collection - all works, but i cant do anything in DataGridView. what I want - it's to load all db in DataGridView than modify its entities and change them (mean add, del or update) and than just save it using DbContext.SaveChanges() - like described in mny of tutorials that i seen - but i cant due to existing of exception Commented Jan 13, 2014 at 19:20

2 Answers 2

2

Is it winforms? if yes, you have to use BindingSource to link data

        BindingSource bs = new BindingSource();
        bs.DataSource = typeof(Book); // Book is a type of your Entity class

        db.Book.ToList().ForEach(n => bs.Add(n)); 
        dgv.DataSource = bs;

now it's editable. And to store changes just call db.SaveChanges();

your way to assing List to DataGrid is valid for WPF. What about to migrate there?

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, i use WinForms, I'm thinking about wpf, but currently i'm just studing (in free from work time) c#. First of all try to understand main milestones. Your proposal perfect - as I understand I must to add some "buffer" for data before use it. Thanks
0

I would suggest to use repository pattern with unitofwork pattern which means a repository class which will handle all CRUD operations and unit of work class to commit all the changes together.

2 Comments

Can you provide any link for getting some tutorial or additional information regarding this pattern, if be honestly - never hear about this
codeproject.com/Articles/37155/… For Unitofwork - create T4 template

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.