2

我正在尝试将一些数据加载到 DataGridView 中,但是 DataGridView 没有显示数据(即使它在调试模式下说它有项目)

    protected void PopulateGrid()
    {
        string dataSource = "test.db";
        String connectionString = "Data Source=" + dataSource;
        String selectCommand = "SELECT * FROM test_run";
        SQLiteDataAdapter dataAdapter =
                    new SQLiteDataAdapter(selectCommand, connectionString);

        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);

        dataGridView1.DataSource = ds;
        dataGridView1.Refresh();
    }

或者它不起作用并出现错误:“datareader has been active on this command”

    protected void PopulateGrid()
    {
        SQLiteConnection SQLconnect = new SQLiteConnection();
        SQLiteCommand SQLcommand = default(SQLiteCommand);
        SQLiteDataAdapter SQLAdapt = new SQLiteDataAdapter();
        SQLiteDataReader SQLreader = default(SQLiteDataReader);
        DataTable db = new DataTable();

        SQLconnect.ConnectionString = "Data Source=test.db;MultipleActiveResultSets=True";
        SQLconnect.Open();
        SQLcommand = SQLconnect.CreateCommand();


        SQLcommand.CommandText = "SELECT * FROM test_run;";
        SQLreader = SQLcommand.ExecuteReader();

        SQLAdapt.SelectCommand = SQLcommand;
        SQLAdapt.Fill(db);
        dataGridView1.DataSource = db.DataSet;
        dataGridView1.Refresh();
4

1 回答 1

4
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
//or if you want to use DataTable as DataSource for your grid
dataGridView1.DataSource = ds.Tables[0];
于 2013-09-17T17:35:14.933 回答