Click here to Skip to main content
15,895,557 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.5K   3.2K   147  
Tutorial on the usage of Windows.Forms.DataGrid
' Domain class: Car
' Represents a car instance, complete with license plate (and, hopefully, not a license to kill)
Public Class Car
#Region "data members"
    Private licenseVal As String
    Private typeVal As CarType
    Private priceVal As Integer
#End Region

#Region "Constructors"
Public Sub New(ByVal pLicense As String, ByVal pPrice As Integer, ByVal pType As CarType)
    licenseVal = pLicense
    typeVal = pType
    price = pPrice
End Sub
#End Region

#Region "Database mapping"
Public Shared Function loadFromDataRow(ByVal dr As DataRow) As Car
  Return New Car(dr("license"), dr("price"), CarType.getCarType(dr("typeID")))
End Function

Public Sub updateDataSet(ByVal ds As CarDataSet)
    Dim carTable As DataTable = ds.Tables("Cars")
    Dim myRow As DataRow = carTable.Select("license='" & license & "'")(0)
    myRow("price") = price
End Sub

Public Sub addToDataSet(ByVal ds As CarDataSet)
    Dim carTable As DataTable = ds.Tables("Cars")
    Dim myRow As DataRow = carTable.NewRow
    myRow("license") = license
    myRow("typeID") = type.id
    myRow("price") = price
    carTable.Rows.Add(myRow)
End Sub

Public Sub removeFromDataSet(ByVal ds As CarDataSet)
    Dim carTable As DataTable = ds.Tables("Cars")
    Dim myRow As DataRow = carTable.Select("license='" & license & "'")(0)
    myRow.Delete()
End Sub

#End Region

#Region "Properties"
Public Property license() As String
Get
    Return licenseVal
End Get
Set(ByVal Value As String)
    If (licenseVal Is Nothing) Then
       licenseVal = Value
     Else
       Throw New Exception("License cannot be altered once set")
     End If
End Set
End Property

Public Property price() As Integer
Get
    Return priceVal
End Get
Set(ByVal Value As Integer)
    priceVal = Value
End Set
End Property

Public ReadOnly Property type() As CarType
Get
    Return typeVal
End Get
End Property

Public ReadOnly Property manufacturer() As CarManufacturer
Get
    Return typeVal.manufacturer
End Get
End Property
#End Region
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