Home

Binding a Data Grid View

 

Introduction

This is an example of binding data of a table to a data grid view. Of course, you must have a database:

Imports System.Data.SqlClient

Public Class Exercise

    Private Sub btnLoad_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnLoad.Click
        Using conExercise As SqlConnection = _
   			New SqlConnection("Data Source=(local);" & _
                                          "Database='Exercise';" & _
                                          "Integrated Security=SSPI;")
            Dim strExercise As String = "SELECT * FROM Employees"
            Dim cmdExercise As SqlCommand = New SqlCommand(strExercise, _
							   conExercise)
            Dim sdaExercise As SqlDataAdapter = New SqlDataAdapter(cmdExercise)
            Dim dsExercise As DataSet = New DataSet("ExerciseSet")

            sdaExercise.Fill(dsExercise)
            dgvExercise.DataSource = dsExercise
            dgvExercise.DataMember = dsExercise.Tables(0).TableName
        End Using
    End Sub
End Class

 

 

Home Copyright © 2008-2016, FunctionX, Inc.