In this article I will explain with an example, how to fill (populate) DataTable MySqlDataAdapter in Windows Forms (WinForms) Application using C# and VB.Net.
 
 

Download and Install the MySQL Connector

You will need to download and install the MySQL Connector in order to connect to the MySQL database in Windows Forms Application.
Note: For details on how to download and install the MySQL Connector, please refer my article Download, install and reference MySQL Connector in C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Fill (Populate) DataTable using MySqlDataAdapter in C# and VB.Net
 
I have already inserted few records in the table.
Fill (Populate) DataTable using MySqlDataAdapter in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Form Design

The Form consists of following control:
DataGridView – For displaying data.
Fill (Populate) DataTable using MySqlDataAdapter in C# and VB.Net
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 

Populating DataTable using MySqlDataAdapter and displaying in DataGridView

Inside the Form Load event handler, the BindGrid method is called.
 

BindGrid

Inside the BindGrid method, first the connection is read from App.Config file.
Note: For more details on how to read connection string from App.Config file, please refer my article .Net 4.5: Read (Get) Connection String from App.Config file using C# and VB.Net.
 
The records are fetched from the Customers Table of MySQL database and copied to DataTable object using Fill method of MySqlDataAdapter class.
Finally, DataTable is assigned to the DataSource property of DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.BindGrid();
}
 
private void BindGrid()
{
    string sql = "SELECT CustomerId, Name, Country FROM Customers";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlDataAdapter sda = new MySqlDataAdapter(sql,con))
        {
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                dgvCustomers.DataSource = dt;
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.BindGrid()
End Sub
 
Private Sub BindGrid()
    Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New MySqlConnection(constr)
        Using sda As New MySqlDataAdapter(sql, con)
            Using dt As New DataTable()
                sda.Fill(dt)
                dgvCustomers.DataSource = dt
            End Using
        End Using
    End Using
End Sub
 
 

Screenshot

Fill (Populate) DataTable using MySqlDataAdapter in C# and VB.Net
 
 

Downloads