none
Custom view in DataGridView supporting updateability back to database? RRS feed

  • Question

  • By dragging my tables from Server Explorer onto the design surface of Object Relational Designer, I can create the mapped entity classes in a DataContext. Showing those tables in DataGridView is easy enough, any change done via DataGridView can be updated successfully. However in some view, all columns will be shown but in some other view I would like to fill just some columns and the important thing here is still allow user to change data via DataGridView and update normally. 

    If there was no requirement of updating data, I could use a simple LINQ query and anonymous type initialization with new{} to show the result in the DataGridView. I also know about the option of hiding DataGridView's column but I think it's better to fill just necessary data in the DataGridView.

    E.g: my database table is Customer having columns: ID, Name, Address, Phone

    Now I would like to show just ID and Name (just for example) in the DataGridView but still allow user to 

    edit and save data via the DataGridView.

    Could you let me know if there is something I'm missing about using LinqToSql to achieve what I want here? Thank you very much. I'm new to this forum BTW.

    Update

    At least now I've tried a working solution by creating a new entity via designer, query will filter the entity table for the desired result and this result can be used as DataSource for DataGridView, ensuring that any change will be updated OK. 

    However another situation comes here, now all the columns (I want to show) may be from different tables (even they have parent-child relationship). Showing the read-only data is easy but I would like to show updatable data in DataGridView. Creating a new entity class always requires it to be mapped to one table in database, so I'm not sure how to do this time.

    Saturday, August 1, 2015 2:07 PM

Answers

  • Hi Love.NET Framework,

    I suggest you binding the whole data to DataGridView, Then set the Visible  property of the DataGridview column to false, then the column will hide.

    this.DataGridView1.Columns[0].Visible = false;

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 4, 2015 1:44 AM