DataRow Collection View : DataRow « GUI « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » GUI » DataRow 




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(88)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(320256)
        Me.DataGrid1.TabIndex = 0
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(344273)
        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.EventArgsHandles 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(1002Then
            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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.