1337Atreyu
1337Atreyu

Reputation: 223

Data not displaying in DataGridView

This is an extension of a question that I had asked before. I am trying to execute an SQL Query and display the results in a DataGridView, but even though I am assigning the datasource to the query results and setting AutoGenerateColumns to true, nothing is displaying in the viewer. Any idea what is missing?

private void Query()
    {
        const string ConnectionPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RetentionDB.mdb";

        try
        {
            using (var cn = new OleDbConnection(ConnectionPath))
            using (var cmd = new OleDbCommand("SELECT * FROM RetentionTable WHERE Center = ?", cn))
            {
                // Parameter names don't matter; OleDb uses positional parameters.
                cmd.Parameters.AddWithValue("@p0", getCenter(""));

                var objDataSet = new DataSet();
                var objDataAdapter = new OleDbDataAdapter(cmd);
                objDataAdapter.Fill(objDataSet);

                dataOutput.AutoGenerateColumns = true;
                dataOutput.DataSource = objDataSet;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
            MessageBox.Show(ex.StackTrace.ToString());
        }
    }

Kudos goes to Richard Deeming for helping me with the code thus far.

p.s I have looked around and have seen many questions on this, but none of them seem to apply (at least that I have found)

Upvotes: 0

Views: 160

Answers (2)

kabijoy
kabijoy

Reputation: 303

Try like this:

private void Query() { const string ConnectionPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RetentionDB.mdb";

        try
        {
            using (var cn = new OleDbConnection(ConnectionPath))
            using (var cmd = new OleDbCommand("SELECT * FROM RetentionTable WHERE Center = ?", cn))
            {
                // Parameter names don't matter; OleDb uses positional parameters.
                cmd.Parameters.AddWithValue("@p0", getCenter(""));

                var objDataSet = new DataSet();
                var objDataAdapter = new OleDbDataAdapter(cmd);
                objDataAdapter.Fill(objDataSet,"RetentionTable");

                dataOutput.AutoGenerateColumns = true;
                dataOutput.DataSource = objDataSet.Tables["RetentionTable"];
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
            MessageBox.Show(ex.StackTrace.ToString());
        }
    }

Upvotes: 0

Gratzy
Gratzy

Reputation: 9389

change

 dataOutput.DataSource = objDataSet;

to

 dataOutput.DataSource = objDataSet.Tables[0];

This talks about what you can use as a datasource for a gridview

GridView

Basically

The IList interface, including one-dimensional arrays

The IListSource interface, such as the DataTable and DataSet classes

The IBindingList interface, such as the BindingList class

The IBindingListView interface, such as the BindingSource class

Upvotes: 3

Related Questions