Bunion
Bunion

Reputation: 461

Select Query sql C# how to output the results to a DataGrid

How do I output results from a refined query to a datagrid view called "customerDataGridView"?

string strCon = Properties.Settings.Default.PID2dbConnectionString;

using (OleDbConnection conn = new OleDbConnection(strCon)) {
   conn.Open();
   string strSql = "SELECT  * FROM customer WHERE City =Belfast";
   OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
}

Thanks for any help!

Upvotes: 0

Views: 2422

Answers (3)

Laila
Laila

Reputation: 437

Try the following after your code:

DataTable table = new DataTable();
adapter.Fill(table);
customerDataGridView.AutoGenerateColumns = True
customerDataGridView.Datasource = table;

Upvotes: 0

sgeddes
sgeddes

Reputation: 62841

Try something like this for ASP.Net:

DataSet ds = new DataSet();
OleDbDataAdapter oledbAdapter = New OleDbDataAdapter(strSql, connection);
oledbAdapter.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);
customerDataGridView.DataBind();

Or something like this for WinForms:

DataSet ds = new DataSet();
OleDbDataAdapter oledbAdapter = New OleDbDataAdapter(strSql, connection);
oledbAdapter.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);

Good luck.

Upvotes: 2

rs.
rs.

Reputation: 27427

Try this

DataSet ds = new DataSet();
OleDbDataAdapter oda = New OleDbDataAdapter(strSql, conn);
oda.Fill(ds);
customerDataGridView.DataSource = ds.Tables(0);

For winform control you don't need DataBind()

Upvotes: 0

Related Questions