ASP.NET Listing of Records in Grid View
It is very simple to display the records in a Grid View.
- If you have not downloaded this project,
download
it and open it in Vistual Studio
2005 using Open Website option.
- Add a new form to the project and open it in design view.
- Place a grid view control in a form.
- Write the following highlighted code in the Code Module.
- Set the new form as the Startup form.
- Run the project to have a preview.
Imports System.Data
Partial Class GridViewList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LoadData()
End If
End Sub
' I strongly recommend to create a procedure
' LoadData to bind the grid view.
' Because we need to call this procedure from
' different places of the coding.
Private Sub LoadData()
Try
Dim Sql As String
' Here you may extract records from any
' table from your database.
Sql = "Select * From Users"
Dim dt As DataTable
dt = GetDataTable(Sql)
' These following code is importat to bind
' the records to the Grid View.
GridView1.DataSource = dt
GridView1.DataBind()
Catch ex As Exception
ShowError(ex)
End Try
End Sub
End Class
You should get the output just like the one given below:
|