0

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!

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

under ".databind();" and "ds.Tables(0);" i get an eror saying "Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'Databind' and no extension method 'Databind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) "
i dont seem to have anything like that, the closest thing to DataBind i have is DataBindings, maby im missing a libary?
Are you using WinForms? If so, check this link out: msdn.microsoft.com/en-us/library/fbk67b6z.aspx
yes, sorry i didnt mention it in the orginal post. ok thanks il check it out
0

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

Comments

0

Try the following after your code:

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

2 Comments

under ".databind();" i get an eror saying "Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'Databind' and no extension method 'Databind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) "
Try removing the Databind(), you may not need it in windows forms. But make sure to set the dataGridView1.AutoGenerateColumns = True. Referring to this: stackoverflow.com/questions/1449773/…. They say that the datasource handles the binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.