Express Yourself with Expression-based Columns Sample 1

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents gridCustOrders As System.Windows.Forms.DataGrid
    Friend WithEvents cboYears As System.Windows.Forms.ComboBox
    Friend WithEvents cboEmployees As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.gridCustOrders = New System.Windows.Forms.DataGrid()
        Me.cboYears = New System.Windows.Forms.ComboBox()
        Me.cboEmployees = New System.Windows.Forms.ComboBox()
        CType(Me.gridCustOrders, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.gridCustOrders.DataMember = ""
        Me.gridCustOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.gridCustOrders.Location = New System.Drawing.Point(8, 40)
        Me.gridCustOrders.Name = "gridCustOrders"
        Me.gridCustOrders.Size = New System.Drawing.Size(672, 256)
        Me.gridCustOrders.TabIndex = 1
        Me.cboYears.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboYears.Location = New System.Drawing.Point(8, 8)
        Me.cboYears.Name = "cboYears"
        Me.cboYears.Size = New System.Drawing.Size(152, 21)
        Me.cboYears.TabIndex = 2
        Me.cboEmployees.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboEmployees.Location = New System.Drawing.Point(352, 8)
        Me.cboEmployees.Name = "cboEmployees"
        Me.cboEmployees.Size = New System.Drawing.Size(328, 21)
        Me.cboEmployees.TabIndex = 3
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(688, 301)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboEmployees, Me.cboYears, Me.gridCustOrders})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Computed columns..."
        CType(Me.gridCustOrders, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    Public data As DataSet
    Private m_CmdYears As String = "SELECT DISTINCT year(orderdate) AS Year FROM orders"
    Private m_CmdCustomers As String = "SELECT customerid, companyname, city FROM customers"
    Private m_CmdEmployees As String = "SELECT * FROM employees"
    Private m_CmdOrders As String = "SELECT o.customerid, od.orderid, o.employeeid, o.orderdate, SUM(quantity*unitprice) AS price FROM Orders o , [Order Details] od WHERE o.orderid=od.orderid GROUP BY o.customerid, od.orderid, o.employeeid, o.orderdate"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Public Sub ColumnChanged(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
        MsgBox(e.Column.ColumnName + " changed!")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub LoadData()
        ' establish the connection
        Dim conn As SqlConnection = New SqlConnection("DATABASE=northwind;SERVER=localhost;Integrated Security=SSPI;")
        Dim da As SqlDataAdapter
        data = New DataSet()

        ' load years 
        da = New SqlDataAdapter(m_CmdYears, conn)
        da.Fill(data, "Years")

        ' load customers
        da = New SqlDataAdapter(m_CmdCustomers, conn)
        da.Fill(data, "Customers")

        ' load employees
        da = New SqlDataAdapter(m_CmdEmployees, conn)
        da.Fill(data, "Employees")
        Dim dt As DataTable = data.Tables("Employees")
        dt.Columns.Add("EmployeeName", GetType(String), "lastname + ', ' + firstname")

        ' load orders
        da = New SqlDataAdapter(m_CmdOrders, conn)
        da.Fill(data, "Orders")

        ' set relations
        Dim rel As DataRelation
        rel = New DataRelation("CustToOrders", _
            data.Tables("Customers").Columns("customerid"), _
        data.Tables("Customers").Columns.Add("Total", GetType(Decimal), "sum(child(CustToOrders).price)")

        ' Save to XML
        data.WriteXml("data.xml", XmlWriteMode.WriteSchema)

        ' data binding
        cboYears.DataSource = data.Tables("Years")
        cboYears.DisplayMember = "Year"

        cboEmployees.DataSource = data.Tables("Employees")
        cboEmployees.DisplayMember = "EmployeeName"

        gridCustOrders.DataSource = data
        gridCustOrders.DataMember = "Customers"
    End Sub
End Class