Qutaiba
Qutaiba

Reputation: 145

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();

Upvotes: 1

Views: 700

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125227

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.

Upvotes: 3

Related Questions