Andrea Visnenza Andy Andrea Visnenza Andy - 3 months ago 18x
SQL Question

why does my datagridview not show any results?

I'm new to and am trying to display results from a mysql server in my datagridview. Can anyone maybe point out whats wrong with my code? Thanks in advance.

Leaving out my connection string for safety, but it does connect. And if I put a breakpoint in the code it does fill the dataset with the correct data

Dim con As New MySqlConnection("server=;user id=;password=;database=")

Dim adp As New MySqlDataAdapter("Select CONCAT(FirstName,' ',Surname) as Name, LeaveDaysAvailable as 'Leave Days Available' from leave_database.Employees;", con)

Dim ds As New DataSet()

dgvMain.DataSource = ds


This is the result. No errors....
enter image description here

this is the result if i run the same query in mysql workbench

enter image description here


A DataSet doesn't contain data directly. It has a Tables collection that contains DataTable objects and each of those contains data. If you assign a DataSet to the DataSource property of your DataGridView, you need to also set the DataMember property to tell it which DataTable to get the data from.

The problem is, you haven't named your DataTable so you have nothing to assign to the DataMember. This line:


actually creates a DataTable, populates it and adds it to the Tables collection. It has no name though, so you have no name to set the DataMember to. You would need to name the DataTable first, e.g.

adp.Fill(ds, "MyDataTable")

and then you can set the DataMember:

dgvMain.DataMember = "MyDataTable"
dgvMain.DataSource = ds

The alternative is to just bind the DataTable instead of the DataSet, in which case you don't need a name:

dgvMain.DataSource = ds.Tables(0)

The thing is though, why create a DataSet in the first place if you're just going to use one DataTable? Simply create a DataTable. You can then pass that DataTable to the Fill call and assign it to the DataSource property.