Windows Forms: How to setup a datagridview to use an inner join SQL Query?
I want the Datagridview to display results based on a simple inner join.
I start by adding the gridview to the form, then configuring a data source. At this stage I can select the tables - all good. It then creates some non visual components on the form.
Now how do I get it to display results f开发者_如何学运维rom the custom sql query I want to pass it?
Well I'm going to go ahead and assume you want your gridview to display a custom sql query.
Step 1: Start by adding a DataSet.xsd to your project.
Step 2: Right click on the designer field and select "Add ---> Tableadapter". It will prompt you to connect to your datasource.
Step 3: On the third menu, select "Use SQL statements".
Step 4: Enter in the desired custom SQL query. Click "Next ---> Next --> Finish"
Step 5: Return to the gridview and click on the extended properties pane (that arrow in the upper right corner). Expand "Other Data Sources" ---> "Project Data Sources" --> DataSet --> and select the datatable you created no more than a minute ago.
This will allow you to display a custom SQL query in the gridview.
Let me know if you need any more clarification!
EDIT: Glad it worked out for you. If you wish to swap the code for dynamic grid functionality, right click on your datatable and add a new query. Then go locate the line in the form_load that takes care of the data population:
this.yourTableAdapter.Fill(this.yourDataSet.yourdatatable);
The default SQL query is always called .Fill, but in case you add a new query the second one will be called:
this.yourTableAdapter.FillBy(this.yourDataSet.yourdatatable);
By running this second method somewhere in your code (a button press maybe) the grid should change to the new sql command and display the new results.
精彩评论