• September 25, 2021


Bring Technology Ideas with you

 Load data from MySQL Server Database into Gridview using VB.net Programming

Instead of putting everything on Form Load. You can put a Sub and write the code to populate Gridview after that you can call in to use in Form Load Event.

Sub loadData()
        Dim select_Data As String
        select_Data = "SELECT * FROM student_info" 'Select data from mysqlserver With the table name Is student infor
        Dim Adapter As MySqlDataAdapter
        Adapter = New MySqlDataAdapter(select_Data, cn) 'put data into memory
        Dim cmd As MySqlCommandBuilder
        cmd = New MySqlCommandBuilder(Adapter) ' execute table with filling from memory storing
        Dim showtable As DataTable
        showtable = New DataTable() 'create a New table to get data
        Adapter.Fill(showtable) 'fill data into the New table
        dtStudentInfo.DataSource = showtable ' show table In datagridviews
    End Sub

After that you call in to use in Form Load Event as below:

Private Sub StudentInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connection() 'call connection to mysql server
        loadData() ' select data from table student information

    End Sub

Design the form as below:

    Rename the Data Gridview to the same as code below if you want to copy and run. Please check our previous tutorial for how-to-connect-vb-net-with-mysql-server-database

    See the result:

      Related post