by Administrator
28. January 2008 13:35
I had some one ask me an interesting question about using linq with the datagridview
When I bind a datagridview to this query
Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}
Dim query = From s In names _
Order By s _
Select s
Dim bs As New BindingSource
bs.DataSource = query
DataGridView1.DataSource = bs
Why do I get these results?
The answer the datagridview will show the properties of the class in the list bound to the datagridview. In this case we are bound to a list of string and the only bindable property is its Length.
If you change the query to this
Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}
Dim query = From s In names _
Order By s _
Select New With {.Name = s}
Dim bs As New BindingSource
bs.DataSource = query
DataGridView1.DataSource = bs
You will get the expected results
I should also mention that you can not bind a datagridview to a linq query directory. You need to bind the query to a bindingsource and bind the datagridview to the bindingsource or bind the datagridview to the queries ToList method.