DataGridView become ReadOnly with Linq using c#

只愿长相守 提交于 2019-12-29 09:18:30

问题


I am implementing a system using C# ( Windows Forms ) with Entity Frame work and Linq.

The DataGridView works well when using normal linq statement, but when I used "Join", the DataGridView Become read Only and I cant edit anything.

Example: DataGridView work well with this statement :

var query = from d in db.Device select d;
        dgvDevices.DataSource = query.ToList();

but it become non-editable with this query :

var query = from u in db.Users join d in db.Device on u.id equals d.AssignedTo 
            select new { d.id, d.Name, d.OwnerName,
                         d.OwnerPhoneNumber, d.Details, User = u.Name };
dgvDevices.DataSource = query.ToList();

回答1:


It is because of nature of anonymous types that are read only.

Anonymous Types (C# Programming Guide)

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

To overcome this limitation you can create your own class and shape the output of query with your own class.



来源:https://stackoverflow.com/questions/32871589/datagridview-become-readonly-with-linq-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!