EntityFramework one-to-many datagridview RRS feed

  • Question

  • Hi all,
    I have two tables on sqlserver (and also in the edmx file):
    Cust (IDcust, Name, Surname)
    Orders (IDord, Product, IDcust)

    From a dropdown choose a customer (in fact SelectedValue returns IDcust)
    and would like to see his orders in a datagrid, (ie Orders in a datagridview).

    at the page I wrote:
    protected ordersEntities context;

    I wrote in the page load:
    context = new ordersEntities();
    ObjectQuery<Cust> query = context.Cust.Include("Orders");
    bindingSource.DataSource = query;
    dgv.DataSource = bindingSource;

    Everything works, I display them.. but I would like to edit/add/remove records in datagrid
    and, if I add a record, I would like the datagrid automatically put the id of the customer...
    how to do this without adding textbox or something?

    thanks a lot


    Tuesday, January 22, 2013 9:09 AM

All replies

  • Hi,

    I think if you want to know the next IDcust, you are required to connect to database. I think you can use a fake IDcust first, which is prepared when first time you query the database. Then you can use this fake IDcust to display. When you create a customer entity object, ignore the fake IDcust and do not assign any value to IDcust. After you SaveChanges, update the correct value of IDcust for DataGridView.

    Thursday, January 24, 2013 5:48 AM