如何使用数据集将数据导入到datagrid? [英] How to get data into datagrid using dataset?

查看:123
本文介绍了如何使用数据集将数据导入到datagrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有什么问题?我想填充datagridview1中的所有用户,但datagirdview不显示任何东西。

whats wrong with this code ? i want populate all users in datagridview1 but datagirdview showing nothing .

private void button4_Click( object sender, EventArgs e )
{
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = db.todosUsuario("select usuario from usuarios");
}

//DB class
public DataSet todosUsuario(string query)
{
    DataSet dt= new DataSet();
    try
    {
        MySqlConnection cnn = new MySqlConnection(MysqlConnect());
        cnn.Open();
        MySqlDataAdapter da = new MySqlDataAdapter(query, cnn);
        da.Fill(dt,"Usuario");
        cnn.Close();
    }
    catch(Exception ed)
    {
        MessageBox.Show(ed.ToString());
    }
    return dt;
}


推荐答案

一个DataSet。

在数据集中有多个表,因此您需要使用DataMember属性指定应在DataGrid中显示哪个表。 (在表集合中只有一个时也是是)

You are binding the DataGridView to a DataSet.
In a dataset there are multiple tables so you need to specify which table should be shown in the DataGrid using the DataMember property. (Yes also when you have only one in the tables collection)

dataGridView1.AutoGenerateColumns = true; 
DataSet ds = db.todosUsuario("select usuario from usuarios"); 
dataGridView1.DataMember = ds.Tables[0].TableName;
dataGridView1.DataSource = ds;

您可以将代码绑定更改为单个表

You could change the code binding to a single table

dataGridView1.DataSource = ds.Tables[0];

这篇关于如何使用数据集将数据导入到datagrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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