Setting up data set and display it

Before we display some data in DataGridView control in Visual Basic window we must create a data set. Data Set is an object that can hold data tables where all the data is stored in rows and columns.

the first step is to create a table.

Dim  dataTableObjectName as DataTable = New DataTable(tableName)

this will create a table which is reference using the dataTableObjectName

Example

Dim userTable As DataTable = New DataTable("Users")

Next step is adding columns to the table

Dim column as DataColumn

dataTableObjectName.Columns.Add (columnName, columnType)

Example

userTable.Columns.Add("userID", Type.GetType("System.Int.32"))
' adding second and third column
userTable.Columns.Add("userName", Type.GetType("System.String"))
userTable.Columns.Add("userContact", Type.GetType("System.String"))

once we we columns in the table we can add row The rows contains records or the data for each user.

dataTableObjectName.Rows.Add(row)

but before that, we have to specify the content of the row . In this step we set the value for each column in new row. First we declare a variable for DataRow, and later use the data row variable to set the value for the columns.

Dim row as DataRow = dataTableObjectName.NewRow()

row(columnName) = value

example

Dim row As DataRow = userTable.NewRow()
row("UserID") = 1
row("UserName") = "John Doe"
row("UserContact") = "0123456789"


Now that we already set up the data table, we add it to data set. We can skip this step and display the data table in data grid view if we want. But normally we add the tables into data set (our local database) before using them.

The syntax:

dataSetName.Tables.add(tableName)

Example

userDataSet.Tables.add(userTable)


Display the data set in data grid view control. To do that we set the DataSource property of data grid view control.

Syntax

dataGridView1.DataSource = dataSetName.Tables(tableName)

Example

userGridView.DataSourse = userDataSet.Tables("Users")



Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net