views:

670

answers:

4

When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to reorder the classes for the purposes of having the resultant columns in a DataGridView appear in a different order. Unfortunately this seems to be exceedingly difficult; you need to cut and paste properties about, or delete them and re-insert them manually.

I know you can reorder columns fairly easily in a DataGridView, however that would result in a lot of hardcoding and I want the designer to match up to the grid.

Does anyone know of any easier way of achieving this or is cutting/pasting the only available method?

I tried manually editing the .designer.cs file, but reordering properties there doesn't appear to do anything!

Edit: Just to make it clear - I want to reorder what's in the LINQ to SQL designer, not what's in the table. I haven't made an error in ordering requiring a reversion to the original table layout; rather I have a table which I want to possess a different ordering in Visual Studio than in SQL Server.

A: 

If you are in the scenario where you have reordered the columns in the database, and you now want to have this new order be reflected in the designer, I think that you have to delete the table from the designer and then put it in again. Or if you use SqlMetal to generate your Linq-to-Sql classes, rerun it on your database and use the newly generated file.

Yaakov Ellis
A: 

@Yaakov: How would deleting+putting in again help me re order columns?... What's SqlMetal?

kronoz
A: 

@Yaakov: Sorry, perhaps I mis-stated the question - I want to reorder the original table layout so it appears in a different order in the DataGridView than in the table.

kronoz
+1  A: 

Using Linq-to-Sql, you can have columns in the DataGridView appear different than in the original table by:

  1. In your Linq query, extract the columns that you want, in the order than you want, and store them in a var. Then the autogenerate columns should show them in that order in the DataGridView
  2. Use Template columns in your DataGridView
  3. Do not use drag-and-drop on the Linq-to-Sql design surface to create your entities. Rather, create them by hand and associate them with the database table using table and column properties

As far as I know, there is no drag-and-drop column reorder in the designer itself

Yaakov Ellis