简体   繁体   中英

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.

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

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

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());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM