DataRow Collection View : DataRow « GUI « VB.Net






DataRow Collection View

  
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private dtSet As DataSet
    Private custTable As DataTable
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Public Sub New()
        MyBase.New()

        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(320, 256)
        Me.DataGrid1.TabIndex = 0
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(344, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        custTable = New DataTable("Customers")
        Dim dtColumn As DataColumn
        Dim myDataRow As DataRow

        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.Int32")
        dtColumn.ColumnName = "id"
        dtColumn.Caption = "Cust ID"
        dtColumn.ReadOnly = False
        dtColumn.Unique = True

        custTable.Columns.Add(dtColumn)

        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.String")
        dtColumn.ColumnName = "Name"
        dtColumn.Caption = "Cust Name"
        dtColumn.AutoIncrement = False
        dtColumn.ReadOnly = False
        dtColumn.Unique = False

        custTable.Columns.Add(dtColumn)

        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.String")
        dtColumn.ColumnName = "Address"
        dtColumn.Caption = "Address"
        dtColumn.ReadOnly = False
        dtColumn.Unique = False

        custTable.Columns.Add(dtColumn)

        Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
        PrimaryKeyColumns(0) = custTable.Columns("id")
        custTable.PrimaryKey = PrimaryKeyColumns

        dtSet = New DataSet("Customers")
        dtSet.Tables.Add(custTable)


        myDataRow = custTable.NewRow()
        myDataRow("id") = 1001
        myDataRow("Address") = "CA"
        myDataRow("Name") = "G"
        custTable.Rows.Add(myDataRow)
        myDataRow = custTable.NewRow()
        myDataRow("id") = 1002
        myDataRow("Name") = "R"
        myDataRow("Address") = "CA"
        custTable.Rows.Add(myDataRow)
        myDataRow = custTable.NewRow()
        myDataRow("id") = 1003
        myDataRow("Name") = "A"
        myDataRow("Address") = "CA"
        custTable.Rows.Add(myDataRow)

        Dim rows As DataRowCollection = custTable.Rows

        Dim counter As Integer
        counter = rows.Count
        Dim row As DataRow
        If rows.Contains(1002) Then
            row = rows.Find(1002)
            rows.Remove(row)
            MessageBox.Show("Row Removed")
        Else
            MessageBox.Show("Row not found")
        End If

        Dim newRow As DataRow
        newRow = custTable.NewRow()
        newRow("id") = 1005
        newRow("Address") = "New Address"
        newRow("Name") = "New Name"
        rows.InsertAt(newRow, 3)

        If (Not rows(1).IsNull("id")) Then
            rows.RemoveAt(1)
        End If
        MessageBox.Show(counter.ToString())
        DataGrid1.DataSource = dtSet.DefaultViewManager

    End Sub

End Class

   
    
  








Related examples in the same category