Show DataReader results in DataGridView

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need to view DataReader results for quick debug.
I tried code below but DataGridView is empty.

How to fix ?

Andrus.

using System.Data;
using System.Windows.Forms;
using Npgsql;

class Form1 : Form
{
static void Main()
{
Application.Run(new Form1());
}

DataGridView dataGridView1 = new DataGridView();

Form1()
{
string s = "SERVER=localhost;USER=postgres";
string query = @"select * from pg_tables";
var con1 = new NpgsqlConnection(s);
IDbCommand cmd = con1.CreateCommand();
cmd.CommandText = query;
con1.Open();
var dr = cmd.ExecuteReader();
dataGridView1.DataSource = dr;
Controls.Add(this.dataGridView1);
}
}
 
Back
Top