This tutorial is a continuation of the tutorial described in Lesson 1: Creating a table viewer.
The complete application can be found in the ADO.NET project %SQLANYSAMP12%\SQLAnywhere\ADO.NET\SimpleViewer\SimpleViewer.sln.
In this tutorial, you add a datagrid control to the form developed in the previous tutorial. This control updates automatically as you navigate through the result set.
Start Visual Studio and load your MySimpleViewer project. This project was created in Lesson 1: Creating a table viewer.
Right-click DataSet1 in the Data Sources window and click Edit DataSet With Designer.
Right-click an empty area in the DataSet Designer window and click Add » TableAdapter.
In the TableAdapter Configuration Wizard:
On the Choose Your Data Connection page, click Next.
On the Choose A Command Type page, click Use SQL Statements, then click Next.
On the Enter A SQL Statement page, click Query Builder.
On the Add Table window, click the Views tab, then click ViewSalesOrders, and then click Add.
Click Close to close the Add Table window.
Expand the Query Builder window so that all sections of the window are visible.
Expand the ViewSalesOrders window so that all the checkboxes are visible.
Click Region.
Click Quantity.
Click ProductID.
In the grid below the ViewSalesOrders window, clear the checkbox under Output for the ProductID column.
For the ProductID column, type a question mark (?) in the Filter cell. This generates a WHERE clause for ProductID.
A SQL query has been built that looks like the following:
SELECT Region, Quantity FROM GROUPO.ViewSalesOrders WHERE (ProductID = :Param1) |
Modify the SQL query as follows:
Change Quantity
to SUM(Quantity) AS TotalSales
.
Add GROUP BY Region
to the end of the query following the WHERE clause.
The modified SQL query now looks like this:
SELECT Region, SUM(Quantity) as TotalSales FROM GROUPO.ViewSalesOrders WHERE (ProductID = :Param1) GROUP BY Region |
Click OK.
Click Finish.
A new TableAdapter called ViewSalesOrders has been added to the DataSet Designer window.
Click the form design tab (Form1).
Stretch the form to the right to make room for a new control.
Expand ViewSalesOrders in the Data Sources window.
Click ViewSalesOrders and click DataGridView from the dropdown list.
Click ViewSalesOrders and drag it to your form (Form1).
A datagrid view control appears on the form.
Build and run the project.
Click Build » Build Solution.
Click Debug » Start Debugging.
In the Param1 text box, enter a product ID number such as 300 and click Fill.
The datagrid view displays a summary of sales by region for the product ID entered.
You can also use the other control on the form to move through the rows of the result set.
It would be ideal, however, if both controls could stay synchronized with each other. The next few steps show how to do this.
Shut down the application and then save your project.
Delete the Fill strip on the form since you do not need it.
On the design form (Form1), right-click the Fill strip to the right of the word Fill, then click Delete.
The Fill strip is removed from the form.
Synchronize the two controls as follows.
On the design form (Form1), right-click the ID text box, then click Properties.
Click the Events button (it appears as a lightning bolt).
Scroll down until you find the TextChanged event.
Click TextChanged, then click FillToolStripButton_Click from the dropdown list. If you are using Visual Basic, the event is called FillToolStripButton_Click.
Double-click FillToolStripButton_Click and the form's code window opens on the fillToolStripButton_Click
event handler.
Find the reference to param1ToolStripTextBox
and change this to iDTextBox
. If you are using Visual Basic, the text box is called IDTextBox
.
Rebuild and run the project.
The application form now appears with a single navigation control.
The datagrid view displays an updated summary of sales by region corresponding to the current product as you move through the result set.
You have now added a control that updates automatically as you navigate through the result set.
Shut down the application and then save your project.
In these tutorials, you saw how the powerful combination of Microsoft Visual Studio, the Server Explorer, and the SQL Anywhere .NET Data Provider can be used to create database applications.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |