LINQ and binding to dataGrid

W

wildThought

Playing around with LINQ and:

SqlConnection con = new SqlConnection(@"Data Source=.
\SQLExpress;Initial Catalog=Northwind;Integrated Security=SSPI");
Northwind db = new Northwind(con);

var q =
from c in db.Employees
where c.EmployeeID == 1
select new { c.FirstName, c.LastName, c.HireDate, c.HomePhone };

dataGridView1.DataSource = q;


This produces a grid as Last Name, Hire Date, First Name, Home Phone.

Why is it a different order then my anonymous type? How can I
influence the order of the dataGrid?
 
N

Nicholas Paldino [.NET/C# MVP]

wildThought,

It's really unadvisable to pass your anonymous types outside of your
method like this. The specification for C# 3.0 does not guarantee the order
of properties in the class that is generated (as you can see).

Additionally, for the DataGrid, you really should set the properties on
your grid to determine the order of the headers. AFAIK, the datagrid will
use the properties in the order reflection will give you, and since this is
an anonymous type, you really have no say in the order in which the
properties are laid out.

You can get away with using the anonymous type, but you have to set the
data grid up with the property names in the order you want to see them
(through the TableStyles and ColumnStyles).

Hope this helps.
 
E

Egghead

Hi here

Beside, you should be able to use "order by" in your LINQ.

cheers,
RL



Nicholas Paldino said:
wildThought,

It's really unadvisable to pass your anonymous types outside of your
method like this. The specification for C# 3.0 does not guarantee the
order of properties in the class that is generated (as you can see).

Additionally, for the DataGrid, you really should set the properties on
your grid to determine the order of the headers. AFAIK, the datagrid will
use the properties in the order reflection will give you, and since this
is an anonymous type, you really have no say in the order in which the
properties are laid out.

You can get away with using the anonymous type, but you have to set the
data grid up with the property names in the order you want to see them
(through the TableStyles and ColumnStyles).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

wildThought said:
Playing around with LINQ and:

SqlConnection con = new SqlConnection(@"Data Source=.
\SQLExpress;Initial Catalog=Northwind;Integrated Security=SSPI");
Northwind db = new Northwind(con);

var q =
from c in db.Employees
where c.EmployeeID == 1
select new { c.FirstName, c.LastName, c.HireDate, c.HomePhone };

dataGridView1.DataSource = q;


This produces a grid as Last Name, Hire Date, First Name, Home Phone.

Why is it a different order then my anonymous type? How can I
influence the order of the dataGrid?
 
W

wildThought

Hi here

Beside, you should be able to use "order by" in your LINQ.

cheers,
RL

Order By? That effects the order of the result set not the order of
the columns. Matt Warren a LINQ developer suggested that it is a
problem and they are working on it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top