How to get data in datagrid using dataset?

What's wrong with this code? I want to populate all users in datagridview1, but datagirdview is 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;
}

      

0


source to share


2 answers


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

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

      



You can change the binding of the code to a separate table

dataGridView1.DataSource = ds.Tables[0];

      

+1


source


The following might be considered a great comment and not an answer, so I made a CW answer.

The code presents a number of problems. They represent good habits to lose early.

private void button4_Click(object sender, EventArgs e)
{
    try
    {
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = db.todosUsuario("select usuario from usuarios");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Exception");
    }
}

//DB class
public DataSet todosUsuario(string query)
{
    DataSet dt = new DataSet();
    using (MySqlConnection cnn = new MySqlConnection(MysqlConnect()))
    {
        cnn.Open();
        using (MySqlDataAdapter da = new MySqlDataAdapter(query, cnn))
        {
            da.Fill(dt, "Usuario");
        }
        cnn.Close();
    }
    return dt;
}

      



Both connection and data adapter objects implement IDisposable

, so they should be wrapped in blocks using

.

Also, it is not recommended to use every method to throw exceptions, especially when the only "handling" you can do is display an error message. Specifically, by placing the call MessageBox.Show

in your DB class, you need it to run in a Windows Forms application and break separation of concerns.

Instead, I suggest that you put exception handling at a very high level in your code. In the case of a Windows Forms application, it is best to store exception handling in event handlers so that exceptions are thrown out of events. Better yet would be to do this last place globally, but I've never done this in a WinForms application, so I can't show you how.

-1


source







All Articles