Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

winforms - DataGridView become ReadOnly with Linq using c#

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...