Sie sind auf Seite 1von 1

Load data from database into DataGridView

To load data from database to DataGridView useDataSource property. The DataSource property is used
for displaying data.
DataGridView1.DataSource = ds.Tables(0)

Drag and Drop one DataGridView control and one Button control on the form. The form looks like this.

Now double click on the Button control and add the following vb.net code.

Imports System.Data.SqlClient
Imports System.Data;
Public Class Form1
Private Sub Button1_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) HandlesButto
n1.Click
MySqlConnection con =
new MySqlConnection(@"server=localhost;user id=root;password=;database=exdb");
con.Open();
MySqlDataAdapter adp = new MySqlDataAdapter("Select * from table1;" ,con);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataBind();
End Sub
End Class

Now run the application and click on the Button.

Das könnte Ihnen auch gefallen