473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

LINQ and binding to dataGrid

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?

Mar 16 '07 #1
3 2908
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]
- mv*@spam.guard.caspershouse.com

"wildThought" <an******@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
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?

Mar 17 '07 #2
Hi here

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

cheers,
RL

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uG**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"wildThought" <an******@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
>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?


Mar 19 '07 #3
On Mar 19, 12:15 pm, "Egghead" <robertlo@NO_SHAW.CAwrote:
Hi here

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

cheers,
RL

"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.comwrote in
messagenews:uG**************@TK2MSFTNGP03.phx.gbl. ..
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]
- m...@spam.guard.caspershouse.com
"wildThought" <andyb...@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
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?
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.

Mar 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
8
by: Richard L Rosenheim | last post by:
I have a dataset containing a parent table related to a child table. The child table contains an ID field (which is configured as autonumber in the datatable), the ID of the parent, plus some...
5
by: Brad Shook | last post by:
I am trying to bind one column of a datagrid to a seperate textbox and the rest of the fields to a datagrid. the comments are too large to fit in a datagrid so I created a textbox below the...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
1
by: john | last post by:
LINQ works a lot like an Access DB Recordset in the way it functions, one of the things I could do in Access is refer to the Fields thru a Variable as below allowing me to Iterate over the...
3
by: Dean Slindee | last post by:
I'm confused. After all these years of promoting 3-tier architecture, isn't LINQ moving us back to a 2-tier architecture (no stored procedures?). Are there scenarios where LINQ would be a better...
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
1
by: jbot | last post by:
I've got some pages that use linq to sql. When I look at the generated sql statements in sql server profiler, I see the same statements repeated. In the example below (from a linq data source...
5
by: Andy B | last post by:
I have been told that I should start using linq. I am used to datasets and how they work. Should I really change since I don't know anything about linq?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.