Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / Visual Basic

DataGrid101: Using Windows.Forms DataGrid

Rate me:
Please Sign up or sign in to vote.
4.70/5 (73 votes)
14 May 2003CPOL8 min read 601.6K   3.2K   147  
Tutorial on the usage of Windows.Forms.DataGrid
Public Class Form1
    Inherits System.Windows.Forms.Form

#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 CarsDbDataAdapter As System.Data.OleDb.OleDbDataAdapter
    Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents CarsDbConnection As System.Data.OleDb.OleDbConnection
    Friend WithEvents MyCarsDataSet As SingleQueryCars.DataSet1
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents MyStyle As System.Windows.Forms.DataGridTableStyle
    Friend WithEvents LicenseColumn As System.Windows.Forms.DataGridTextBoxColumn
    Friend WithEvents ManufacturerCol As System.Windows.Forms.DataGridTextBoxColumn
    Friend WithEvents TypeCol As System.Windows.Forms.DataGridTextBoxColumn
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CarsDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter()
Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand()
Me.CarsDbConnection = New System.Data.OleDb.OleDbConnection()
Me.MyCarsDataSet = New SingleQueryCars.DataSet1()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.MyStyle = New System.Windows.Forms.DataGridTableStyle()
Me.LicenseColumn = New System.Windows.Forms.DataGridTextBoxColumn()
Me.ManufacturerCol = New System.Windows.Forms.DataGridTextBoxColumn()
Me.TypeCol = New System.Windows.Forms.DataGridTextBoxColumn()
Me.Label1 = New System.Windows.Forms.Label()
CType(Me.MyCarsDataSet, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'CarsDbDataAdapter
'
Me.CarsDbDataAdapter.SelectCommand = Me.OleDbSelectCommand1
Me.CarsDbDataAdapter.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Cars", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("license", "license"), New System.Data.Common.DataColumnMapping("Cars.typeID", "Cars.typeID"), New System.Data.Common.DataColumnMapping("Types.typeID", "Types.typeID"), New System.Data.Common.DataColumnMapping("typeName", "typeName"), New System.Data.Common.DataColumnMapping("Types.ManufacturerID", "Types.ManufacturerID"), New System.Data.Common.DataColumnMapping("Manufacturer.ManufacturerID", "Manufacturer.ManufacturerID"), New System.Data.Common.DataColumnMapping("ManufacturerName", "ManufacturerName")})})
'
'OleDbSelectCommand1
'
Me.OleDbSelectCommand1.CommandText = "SELECT Cars.license, Cars.typeID, Types.manufacturerID, Types.typeID AS Expr1, Ty" & _
"pes.typeName, Manufacturer.ManufacturerID AS Expr2, Manufacturer.ManufacturerNam" & _
"e FROM Cars,Types,Manufacturer WHERE Cars.typeID = Types.typeID AND Types.manufa" & _
"cturerID = Manufacturer.ManufacturerID"
Me.OleDbSelectCommand1.Connection = Me.CarsDbConnection
'
'CarsDbConnection
'
Me.CarsDbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=../../cars" & _
".mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Je" & _
"t OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;" & _
"Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:" & _
"Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create S" & _
"ystem Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Local" & _
"e on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=" & _
"False"
'
'MyCarsDataSet
'
Me.MyCarsDataSet.DataSetName = "CarsDataSet"
Me.MyCarsDataSet.Locale = New System.Globalization.CultureInfo("he-IL")
Me.MyCarsDataSet.Namespace = "http://www.tempuri.org/DataSet1.xsd"
'
'DataGrid1
'
Me.DataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right)
Me.DataGrid1.DataMember = ""
Me.DataGrid1.DataSource = Me.MyCarsDataSet.Cars
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.DataGrid1.Size = New System.Drawing.Size(264, 216)
Me.DataGrid1.TabIndex = 0
Me.DataGrid1.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {Me.MyStyle})
'
'MyStyle
'
Me.MyStyle.DataGrid = Me.DataGrid1
Me.MyStyle.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {Me.LicenseColumn, Me.ManufacturerCol, Me.TypeCol})
Me.MyStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.MyStyle.MappingName = "Cars"
'
'LicenseColumn
'
Me.LicenseColumn.Format = ""
Me.LicenseColumn.FormatInfo = Nothing
Me.LicenseColumn.HeaderText = "License Plate"
Me.LicenseColumn.MappingName = "license"
Me.LicenseColumn.ReadOnly = True
Me.LicenseColumn.Width = 75
'
'ManufacturerCol
'
Me.ManufacturerCol.Format = ""
Me.ManufacturerCol.FormatInfo = Nothing
Me.ManufacturerCol.HeaderText = "Manufacturer"
Me.ManufacturerCol.MappingName = "ManufacturerName"
Me.ManufacturerCol.ReadOnly = True
Me.ManufacturerCol.Width = 75
'
'TypeCol
'
Me.TypeCol.Format = ""
Me.TypeCol.FormatInfo = Nothing
Me.TypeCol.HeaderText = "Car Type"
Me.TypeCol.MappingName = "typeName"
Me.TypeCol.ReadOnly = True
Me.TypeCol.Width = 75
'
'Label1
'
Me.Label1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right)
Me.Label1.Location = New System.Drawing.Point(0, 216)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(272, 40)
Me.Label1.TabIndex = 1
Me.Label1.Text = "This example shows how a grid can be based on an SQL Join. Note the difference be" & _
"tween the DataSet and the grid columns. This example is read-only."
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(264, 261)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.DataGrid1})
Me.Name = "Form1"
Me.Text = "SQL Join based grid"
CType(Me.MyCarsDataSet, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

    End Sub

#End Region

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Customizing a DataGrid that is based on a single
' dataset "Table" so that it fits the required display
' including Column Headers Colum width, Column sorting and 
' Column hiding (All done in design time):
' (1) Add a style in the "TableStyles" collection
' (2) Map the style (mappingName) to the DataSet table (if in doubt - view schema)
' (3) For each desired column add a GridColumnStyle, and map
' That's all
' Tip: if after phase 2 columns disappear don't worry - you mapped corectly. 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Private Sub OnFormLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CarsDbDataAdapter.Fill(MyCarsDataSet)
    End Sub

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
Web Developer
Israel Israel
Omri started coding way back in the good ol' Apple ][ days. His first commercial (acutally governmental) software package was released 1986.
In the 90's Omri programmed C and C++, and managed larger development teams using MFC/COM.
In the last 3 years he is into Java server programming, and while he loves the language and community, he has many reservations. Maybe this is why .Net feels like comming back home...

Comments and Discussions