简体   繁体   English

数据未显示在DataGridView中

[英]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. 我试图执行SQL查询并在DataGridView中显示结果,但是即使我将数据源分配给查询结果并将AutoGenerateColumns设置为true,查看器中也不会显示任何内容。 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. 到目前为止,Kudos感谢Richard Deeming帮助我编写代码。

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) ps我环顾四周,并看到了许多与此有关的问题,但似乎没有一个适用(至少我发现了)

change 更改

 dataOutput.DataSource = objDataSet;

to

 dataOutput.DataSource = objDataSet.Tables[0];

This talks about what you can use as a datasource for a gridview 这讨论了可以用作gridview的数据源的内容

GridView 网格视图

Basically 基本上

The IList interface, including one-dimensional arrays IList接口,包括一维数组

The IListSource interface, such as the DataTable and DataSet classes IListSource接口,例如DataTable和DataSet类

The IBindingList interface, such as the BindingList class IBindingList接口,例如BindingList类

The IBindingListView interface, such as the BindingSource class IBindingListView接口,例如BindingSource类

Try like this: 尝试这样:

private void Query() { const string ConnectionPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RetentionDB.mdb"; 私有无效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());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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