Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

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.zip
ejemplo
ejemplo
bin
Debug
ejemplo.exe
ejemplo.vshost.exe
ejemplo.vshost.exe.manifest
My Project
Application.myapp
Settings.settings
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

JOSUEYERAY
Software Developer Plain Concepts
Spain Spain
MCP - .NET Framework 2.0 Application Development Fundamentals
MCTS - .NET Framework 3.5 Windows Forms Application
MCTS - .NET Framework 3.5 Windows Presentation Foundation

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 17 May 2010
Article Copyright 2010 by JOSUEYERAY
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid