Datagridview and Linq issue

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?

image

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

image  

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.



kick it on DotNetKicks.com

Tags: , ,

DataGridView | Linq | VB

Comments (7) -

SSNLocator
12/3/2008 2:43:08 AM #

great stuff mate..
thanks for sharing..

Busby SEO Test
12/3/2008 6:58:05 PM #

Nice posting .. thank for info boss

Busby SEO Test Pinay
12/27/2008 8:45:16 AM #

I am using WLW with BE.NET without any problem (well I do not count images created with https URL when posted by Metawebblog API with SSL enabled). If you have not yet tried, give it a second chance with the latest BE.NET 1.4.5.7 and this WLW tech preview

Busby SEO Test
12/27/2008 7:30:19 PM #

closing the gap here.

Busby SEO Test
12/29/2008 2:11:50 PM #

to create datagride view you nead to change the query. thank for share this.

hrer
hrer
3/30/2010 4:01:38 AM #

http://hebe-blue.com/">hebe-blue
http://tennis-athletcs.com/">tennis-athletcs
http://sports-aquatics.com/">sports-aquatics
http://swimming-athletics.com/">swimming-athletics
http://sports-swimming.com/">sports-swimming
http://sports-freestyle.com/">sports-freestyle
http://sports-backstroke.com/">sports-backstroke
http://football-all.net/">football-all
http://sports-butterfly.com/">sports-butterfly
http://baseball-allin.com/">baseball-allin
http://sports-track.net/">sports-track
http://sports-jumping.com/">sports-jumping
http://sports-polevault.com/">sports-polevault7474
http://softball-into.com/">softball-into
http://sports-winston.com/">sports-winston

Comments are closed

About the author

I work at seaworld as a software developer.  

 

Windows phones apps

 

Auto Codes

Points Calculator

Month List

Page List