Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / Visual Basic

Linq To Dataset: Display the Contents of Several Tables in a Data Control

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
17 May 2010CPOL2 min read 32K   554   12  
In several cases, we will be required to work with data in different datasets, or on the same dataset but at different DataTables
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As New DataSet

        'Getting our Data
        ds = GetData()

        'Set the datagrid data source
        DataGridView1.DataSource = (From roworder As DataRow In ds.Tables("Order")
                                    Join rowdetails As DataRow In ds.Tables("Details")
                                        On roworder.Field(Of String)("orderID") Equals rowdetails.Field(Of String)("orderID")
                                    Join rowproducts As DataRow In ds.Tables("Products")
                                        On rowdetails.Field(Of String)("productID") Equals rowproducts.Field(Of String)("productID")
                                    Select New With
                                                    {
                                                        .OrderCode = roworder.Field(Of String)("orderID"),
                                                        .OrderDate = roworder.Field(Of String)("orderDate"),
                                                        .ProductName = rowproducts.Field(Of String)("productDesc"),
                                                        .Quantity = rowdetails.Field(Of String)("quantity"),
                                                        .UnitPrice = rowproducts.Field(Of String)("price"),
                                                        .Total = .Quantity * .UnitPrice
                                                    }).ToList

    End Sub

    Public Function GetData() As DataSet
        'Create the Dataset and add 3 tables
        Dim ds As New DataSet
        ds.Tables.Add("Order")
        ds.Tables.Add("Details")
        ds.Tables.Add("Products")

        'Create the columns for each Table
        'Order:
        ds.Tables("Order").Columns.Add("orderID")
        ds.Tables("Order").Columns.Add("orderDate")

        'Details
        ds.Tables("Details").Columns.Add("orderID")
        ds.Tables("Details").Columns.Add("productID")
        ds.Tables("Details").Columns.Add("quantity")

        'Products
        ds.Tables("Products").Columns.Add("productID")
        ds.Tables("Products").Columns.Add("productDesc")
        ds.Tables("Products").Columns.Add("price")

        'Create dummy rows for our data.
        For i As Integer = 1 To 10
            ds.Tables("Order").Rows.Add(New String() {i.ToString, Date.Now})
            For j As Integer = 1 To 10
                ds.Tables("Details").Rows.Add(New String() {i.ToString, j.ToString, i.ToString})
            Next
            ds.Tables("Products").Rows.Add(New String() {i.ToString, "Producto " + i.ToString, New Random(25 * i).Next.ToString})
        Next
        ds.AcceptChanges()
        Return ds
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelance Developer
Spain Spain
MVP Windows Platform Development 2014
MVP Windows Phone Development 2013
MVP Windows Phone Development 2012

Comments and Discussions