What we have in this page?
| 12. Click the Advanced… button to see advanced properties of the connection. Just left as is and click OK button.
13. Next click the Test Connection button to test the connection to the data source. If there is any connection error, you need to resolve it. Close the Add Connection dialog box.
|
14. You can see the connection string for this connection. If you can’t see it, expand the plus (+) sign next to the Connection string label. Click the Next button.

15. Now we are going to choose the database objects. Expand the plus sign for the Tables and Views folders. You can see all the Tables and Views objects for Northwind.mdb database.

16. The first step is to specify the tables that we want to use. In this case, there’s only one, so expand the Products from the list in the Tables folder. We want to display the product name (ProductName), the unit price (UnitPrice), and the number of units in stock (UnitsInStock), so select these fields from the list. Make sure that you also select the ProductID field as well. We don’t want to display it, but it’s the primary key for the table and the adapter won’t be able to update the table unless you include it. Click the Finish button.

17. Resize the Form and the DataGridView controls appropriately so that you can see all the columns.

18. Build and run you application. The following output should be expected. It seems that by default SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products SQL query has been added.

19. Back to the designer and open the DataGridView Tasks. Here, you can edit existing columns properties, add new column. Preview the data and add new SQL query.

20. We are going to add a button that can sort the UnitPrice ascending. Select the Add Query… menu item. Select New query name: radio button and put MyQuery1 as the query name. Click the Query Builder… button.

21. Select the UnitPrice row and set the Sort Type to Ascending and click the Execute Query button. The SQL query script is generated together with the sorted data. Click the OK button.

22. In the Search Criteria Builder dialog box you can see the generated query text. Click the OK button to dismiss the Search Criteria Builder dialog box.

23. By default a button that contains an event of the new query is added in the ToolStripContainer. Select and right click the MyQuery1 button in the ToolStrip control and select Properties. Set the Text property to Sort Unit Price Asc.
| |
24. Next we need to properly re-arrange the ToolStripContainer that contain the button, at the top of the DataGridView control. Open the ToolStrip Tasks and select the Embed in ToolStripContainer.

25. Next step, clear all the tick boxes except the Top and select Dock Fill in Form.

26. Finally click the Re-parent Controls in the ToolStripContainer Tasks. The button is nicely fitted at the top of the DataGridView control.


27. Build and run your application. When you click the Sort Unit Price Asc, the UnitPrice column will be sorted ascending. Keep in mind that based on the DataGridView properties, you can just sorting ascending or descending any of the columns by clicking the column header.


| To | Do this |
| Use ADO.NET classes. | If you are using Visual Studio/C++ .NET, you will need to add only a using directive for the appropriate data provider. For example:
using namespace System::Data::SqlClient;
|
| Connect to a database. | Create a SqlConnection or OleDbConnection object, and configure its ConnectionString property. |
| Create a command object. | Create a SqlCommand or OleDbCommand object, and configure its CommandText, CommandType, and Connection properties. |
| Execute a command. | If the command returns a scalar value, call ExecuteScalar. If the command modifies the database, call ExecuteNonQuery. If the command performs a query, call ExecuteReader. Assign the result to a SqlDataReader or OleDbReader object, and use this reader to loop through the result set. For example:
OleDbDataReader ^ reader = cmProducts->ExecuteReader(); while (reader->Read()) { Console::Write(reader->GetString(0)); }
|
| Use data in a disconnected application. | Create a SqlDataAdapter (or OleDbAdapter), and specify commands to access the database. Create a DataSet, and fill the DataSet by using the data adapter. For example:
daTitles = gcnew OleDbDataAdapter( L"SELECT * FROM Titles", cnNwind); dsTitles = gcnew DataSet("Titles"); daTitles->Fill(dsTitles);
|
| Display a DataSet in a | Use the DataSource property of DataGridView. For example:
dgTitles->DataSource = dsTitles->Tables->default[0]->DefaultView; |
|
Table 17 | |