C# – LINQ to SQL, anonymous types are read-only

anonymous-typesclinq-to-sql

I'm using LINQ to SQL and DataGridViews to create an editable user interface of a database.
My problem is that I have to translate the column names when fetching the results from the database, because while all names in the database are in English (the only language supported by the visual studio class designer) I have to display them in a different language…

So I thought about using anonymous types:

c_dgvShops.DataSource = (from Shop s in DataContext.Shops
                                 select new
                                     {
                                         Azonosito = s.Shop_ID,
                                         Nev = s.Name,
                                         Megye = s.County,
                                         Iranyitoszam = s.Zipcode,
                                         Cim = s.Address,
                                         Latogatasok_szama = s.Visits.Count
                                     });

(Please note that the property names of the anonymous type are in Hungarian [the language I have to display in], translated from English.)

This resulted in the DataGridViews being read-only.
I made some Google searches, and found this:
http://blogs.msdn.com/swiss_dpe_team/archive/2008/01/25/using-your-own-defined-type-in-a-linq-query-expression.aspx.
Based on this article, I created a new class called 'Bolt' (Hungarian equivalent for Shop) and filled it with the translated property names, so the query became:

c_dgvShops.DataSource = (from Shop s in DataContext.Shops
                                 select new Bolt
                                     {
                                        Azonosito = s.Shop_ID,
                                        Nev = s.Name,
                                        Megye = s.County,
                                        Iranyitoszam = s.Zipcode,
                                        Cim = s.Address,
                                        Latogatasok_szama = s.Visits.Count()
                                     });

Although it's not totally clear to me, how would this make it possible to edit the data displayed in the DataGridView, I tried it, and got an exception:

NotSupportedException: Explicit construction of entity type 'DataTest_L2SQL.Bolt' in query is not allowed.

So what's the correct way of doing this? How can I display alias column names, but still keep the data editable?

Thank you very much in advance!

Best Answer

You should be manually defining the datagrid columns (which would allow you to set their caption to anything you want as well as customize the size and display formatting), not relying on the datagrid to discover your columns through the data source - this feature is only meant for quick and dirty setups.