' Import Data and OleDb namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    ' Declare objects...
    Dim myConnection As SqlConnection = New _
        SqlConnection("server=(local);database=pubs;uid=sa;pwd=")

    Dim myDataAdapter As New SqlDataAdapter()
    Dim myDataSet As DataSet = New DataSet()

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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 grdAuthorTitles As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.grdAuthorTitles = New System.Windows.Forms.DataGrid()
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'grdAuthorTitles
        '
        Me.grdAuthorTitles.DataMember = ""
        Me.grdAuthorTitles.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.grdAuthorTitles.Location = New System.Drawing.Point(16, 16)
        Me.grdAuthorTitles.Name = "grdAuthorTitles"
        Me.grdAuthorTitles.Size = New System.Drawing.Size(528, 296)
        Me.grdAuthorTitles.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(552, 325)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.grdAuthorTitles})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the SelectCommand properties...
        myDataAdapter.SelectCommand = New SqlCommand()
        myDataAdapter.SelectCommand.Connection = myConnection
        myDataAdapter.SelectCommand.CommandText = _
            "SELECT au_lname, au_fname, title, price " & _
            "FROM authors " & _
            "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
            "JOIN titles ON titleauthor.title_id = titles.title_id " & _
            "ORDER BY au_lname, au_fname"
        myDataAdapter.SelectCommand.CommandType = CommandType.Text

        ' Open the database connection...
        myConnection.Open()
        ' Now execute the command...
        myDataAdapter.SelectCommand.ExecuteNonQuery()

        ' Fill the DataSet object with data...
        myDataAdapter.Fill(myDataSet, "authors")

        ' Close the database connection...
        myConnection.Close()

        ' Set the DataGrid properties to bind it to our data...
        grdAuthorTitles.DataSource = myDataSet
        grdAuthorTitles.DataMember = "authors"

        ' Declare objects for the DataGrid...
        Dim objDataGridTableStyle As New DataGridTableStyle()
        Dim objTextCol As New DataGridTextBoxColumn()

        ' Set the AlternatingBackColor property...
        objDataGridTableStyle.AlternatingBackColor = Color.WhiteSmoke

        ' Set the MappingName for the DataGridTableStyle...
        objDataGridTableStyle.MappingName = "authors"

        ' Set the MappingName for the first column...
        objTextCol.MappingName = "au_lname"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Last Name"
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the second column...
        objTextCol.MappingName = "au_fname"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "First Name"
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the third column...
        objTextCol.MappingName = "title"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Book Title"
        ' Set the Width of the column...
        objTextCol.Width = 304
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the fourth column...
        objTextCol.MappingName = "price"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Retail Price"
        ' Set the Alignment within the column...
        objTextCol.Alignment = HorizontalAlignment.Right
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Add the DataGridTableStyle to the DataGrid...
        grdAuthorTitles.TableStyles.Add(objDataGridTableStyle)

    End Sub
End Class
