Rudi
Rudi

Reputation: 936

DataGridView not showing Columns/Data

I'm trying to load some data into a DataGridView, but either the DataGridView is not showing the data (even though it says in Debug-Mode that it has the items in it)

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

or it doesn't work and an error comes up: "datareader already 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();

Upvotes: 2

Views: 6894

Answers (1)

King King
King King

Reputation: 63327

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];

Upvotes: 4

Related Questions