DataGridView不显示我的列 [英] DataGridView not showing my columns

查看:150
本文介绍了DataGridView不显示我的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个MySQL连接,该连接应该将表数据放入DataGridView,但是它有一个问题,它会使我的用户表中每个用户表的行都进入数据库,但未显示实际数据.有人可以帮忙吗?

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?

我的代码:

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];
        }
    }
}

问题:

推荐答案

因为您使用的是DataSet而不是DataTable,所以您需要更改此设置:

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

adapter.Fill(ds);

对此:

adapter.Fill(ds,"tbl");

或者您可以使用 DataTable 代替DataSet:

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

这篇关于DataGridView不显示我的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆