Linq
Since Visual Studio 2008 is due out by the end of the Month I am updating some of my datagridview samples for LINQ.
To start off create a new windows forms application in VS 2008 make sure you select FrameWork 3.5 so you can use linq
I now added a new Linq to Sql designer to the project and named it Northwind. Drag the Northwind Products Table on to the design surface from the Server Explorer.
On your windows forms add a DataGridView (DataGridView1) and a NumericUpDown control (nuPage). For this...
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. ...
Here is a simple linq query which places the numbers from 1 and 150 in a random order
Dim r As New Random(Now.Ticks Mod Int32.MaxValue)
Dim rndLst = From l In (From num In Enumerable.Range(1, 150) _
Select New With {.Num = num, .pos = r.Next(1, 150)}) _
Order By l.pos _
Select l.Num
For Each i In rndLst
Console.WriteLine(i)
Next
Linq allows you to query the data in a dataset. For this example I load the Products and Categories table from the Northwind Database. I then do a join query to export the Product Name, Unit Price, and Category Name into a list which I display in a datagridview. Note a query of this type will not display in a datagridview you need to set the datasource equal to the query's Tolist method.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = _
"Server = .\SQLEXPRESS;Database...
For this example we are going to create a Excel 2007 spreadsheet using the Microsoft OpenXml Sdk and Linq to XML.
To start with lets create a new Windows forms app which targets the .Net Framework 3.5. Add a Linq to Sql design surface to your project and name it Northwind and drag the Northwind Products table on to the surface. On the windows form I added a DataGridview to display the data we are going to export to excel. We also need a button named btnExport on the form.
To create a excel spreadsheet we need to use the...
Using XLinq to get a list of Photos from Spaces.Live.com
Storing photo albums on line is becoming very popular. Spaces.Live.com photo is one place to store albums which exposes its photo albums via an rss feed. I thought it would be nice to test out Visual Studio 2008 XLinq by getting a list of Photo's from the Tampa Code Camp and display them in WPF Listbox.
The photos I am looking for can be found here.
http://thedevfish.spaces.live.com/photos/cns!75364D9E73295107!133/feed.rss
The xml in the rss feed exposes each photo like this
<item>
<title>volunteers arrived at 630am - nikita polyakov [mvp] led them</title>
<link>http://thedevfish.spaces.live.com/photos/cns!75364D9E73295107!133/cns!75364D9E73295107!134</link>
<description><p><a href="http://thedevfish.spaces.live.com/photos/cns!75364D9E73295107!133/cns!75364D9E73295107!134" mce_href="http://thedevfish.spaces.live.com/photos/cns!75364D9E73295107!133/cns!75364D9E73295107!134"><img src="http://storage.live.com/items/75364D9E73295107!134:thumbnail"...
Using Linq for Master Detail in a DataGridView
I saw in the MSDN forums someone asking how to do a master details relationship with Linq. It is actually pretty simple. Here are the steps involved on creating the relationship. I am using VS 2008 Beta 2 for this example.
Open up Visual Studio 2008 Beta 2 and create a windows forms Application
Add a new Linq 2 Sql classes to the project named Northwind.dbml
Drag the Northwind database's Orders and Order Details table onto the surface
Save the project and build it
Open the data sources window and add a new object data source. Select...