How to Use DataGridView for MySQL in VB.NET
Visual Basic .NET uses the "DataGridView" control to lay out data in a tablelike format. Programmers are not required to create the table, but they must query the MySQL database to create a data set. The data set then binds to the DataGridView, and .NET automatically lays out the data on the webpage. The .NET DataGridView is included with the VB.NET library so you can drag and drop a control from the .NET toolbox.
Instructions
-
-
1
Click the Windows "Start" button and select "All Programs." Click "Microsoft .NET Framework," then click "Visual Studio" to open the .NET programming software. Open your web project after Visual Studio loads.
-
2
Double-click the form you want to use to display the data. All VB.NET forms are displayed in Solution Explorer on the right side of the work window. After the form loads in the designer, drag and drop the "DataGridView" control from the toolbox to the form.
-
-
3
Right-click the form and select "View Code." The VB.NET editor opens, which is where you type your VB code. The first code added must be the connection string to the MySQL database. The following code creates a connection to the MySQL server:
Dim conn As String = "Server=localhost;Database=db;Uid=user;Pwd=pass;"
Replace "db" with the name of your MySQL database. Replace "user" and "pass" with your own username and password. If you use a host provider for your MySQL database, the host administrator provides you with this information.
-
4
Query the MySQL database. You use the SQL language to query MySQL. The following code shows you how to query for all customers on the MySQL database:
adapter = New SqlDataAdapter("select * from customers", conn)
-
5
Create a table from the queried information. A table contains all of the records, so your VB.NET code can translate the data to a DataGridView control. The following code creates the table:
Dim dt As New DataTable()
adapter.Fill(dt)
-
6
Assign the table to the DataGridView. When you assign the table to the grid, the data displays to the user. The following code completes the data setup:
customer1.DataSource = dt
-
1