Manually Bind DataGridView From SQL Server Database in VB.Net Windows Form Applications

Leave a Comment
In this article, we will study how to Manually Bind DataGridView From SQL Server Database in VB.Net Windows Form Applications.

In previous article, we study how to insert Windows Forms data into SQL Server Database. We continue previous article in this tutorial.

So, Add New Windows Forms and named as StudentDetails.vb and Drag and Drop DataGridView into Windows Form from the toolbox.


To Bind the DataGridView we have to create OnLoad Event. to create OnLoad Event Double click Windows Forms and add below code -

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class StudentDetails
    Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Developer\Desktop\Kisan\WindowsApplication1\WindowsApplication1\StudentDemo.mdf;Integrated Security=True;User Instance=True")
    Private Sub StudentDetails_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
         Dim dt As DataTable = New DataTable()
        con.Open()
        Dim dap As SqlDataAdapter = New SqlDataAdapter("Select * FROM Student ORDER BY Id DESC", con)
        dap.Fill(dt)
        DataGridView1.DataSource = dt
        con.Close()
    End Sub 
End Class

It' Done

Download Complete Source Code

0 comments:

Post a Comment