Sunday, April 22, 2012

Step to Create Dataset

This sample show the basic step to create dataset and there element. 

Sample Code

Step 1 you should to Imports System.Data

Step 2 please see sample code below.

Dim ds As New DataSet("DataSetSample")

' Create Table
Dim dt As New DataTable("DataTable")

' Create Column
Dim cl1 As New DataColumn("Column1")
cl1.DataType = GetType(Integer)

Dim cl2 As New DataColumn("Column2")
cl2.DataType = GetType(String)

' Add column to table
dt.Columns.Add(cl1)
dt.Columns.Add(cl2)

' Add DataTable to DataSet
ds.Tables.Add(dt)

' Step to add DataRow
' Create new row
Dim dr As DataRow
dr = ds.Tables("DataTable").NewRow

dr("Column1") = 1
dr("Column2") = "Welcome to vb.net Sample code"

' Add Row1
ds.Tables("DataTable").Rows.Add(dr)


' If you can use array of datarow.
Dim drArray(1) As DataRow

drArray(0) = ds.Tables(0).NewRow
drArray(0)("Column1") = 2
drArray(0)("Column2") = "Show easy sample code"
ds.Tables("DataTable").Rows.Add(drArray(0))

drArray(1) = ds.Tables(0).NewRow
drArray(1)("Column1") = 3
drArray(1)("Column2") = "Easy to understand"
ds.Tables("DataTable").Rows.Add(drArray(1))


' binddata to gridview
Me.DataGridView1.DataSource = ds.Tables("DataTable").Copy()


vb.net sample code

No comments:

Post a Comment