Wiooo Wiooo - 1 month ago 10
C# Question

DataGridView not showing my columns

I have a MySQL connection in C# that supposedly puts the table data into a

DataGridView
, but it has a problem and it makes the rows for every account in my users table in my db, but it doesn't show the actual data. Can anyone help?

My code:

public Form1()
{
InitializeComponent();

string connectionString = "Server=mysql.dunkycart.com; Database=dunkycart; Uid=dunkycart; Pwd=rooB-dnK-sqL;"; //Set your MySQL connection string here.
string query = "SELECT name, username, email FROM users"; // set query to fetch data "Select * from tabelname";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
}


The issue:
This is the output of the form. I entered the column names manually, btw, so it's not doing that either.

Answer

Because you are using DataSet instead of DataTable so you need to change this:

adapter.Fill(ds);

To this:

adapter.Fill(ds,"tbl");

Or you can use a DataTable instead of DataSet:

DataTable dt = new DataTable();
adapter.Fill(dt);
Comments