Click here to Skip to main content
12,701,051 members (29,566 online)
Click here to Skip to main content

Tagged as

Stats

22.1K views
503 downloads
12 bookmarked
Posted

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

, 17 May 2010 CPOL
In several cases, we will be required to work with data in different datasets, or on the same dataset but at different DataTables
ejemplo
ejemplo
bin
Debug
ejemplo.exe
ejemplo.vshost.exe
ejemplo.vshost.exe.manifest
My Project
Application.myapp
ejemplo.suo
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)

Share

About the Author

Josué Yeray Julián Ferreiro
Software Developer (Senior) Freelance Developer
Spain Spain
MVP Windows Platform Development 2014
MVP Windows Phone Development 2013
MVP Windows Phone Development 2012

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 17 May 2010
Article Copyright 2010 by Josué Yeray Julián Ferreiro
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid