This section describes the steps required to complete Step 2 of the DBBrowserGrid tutorial. Step 2 changes the application in Step 1 by adding the database access and by binding the DBBrowserGrid control to different data access objects from ADO.NET.
If you want to retain the work done in Step 1, create a separate copy of it.
In the Solution Explorer, right-click the project. In the popup menu, select Add | Add Windows Form… .
Enter FrmAdoObjs.cs as the file name and click Open.
A new form (FrmAdoObjs) is automatically added to the project, and displayed in Design mode. Resize the form as desired.
Select FrmAdoObjs. If the properties for FrmAdoObjs are not already displayed, right-click the form; a context menu is displayed. Select Properties from the context menu.
In the resulting property grid for the form, change the following properties.
Text = Binding to ADO.NET objects
Drag a MainMenu component onto FrmAdoObjs.
Add an item to the main menu with the title "&ADO.NET".
Add items to the ADO.NET submenu with the following titles:
DataSet
DataTable
DataView
Return to the FrmMain designer and add an "ADO.NET objects" item to the Bind to… item of the main menu. Double-click ADO.NET objects. The code for
private void menuItem3_Click(object sender, System.EventArgs e) |
appears. Enter the following code as the function body:
FrmAdoObjs frm = new FrmAdoObjs(); frm.MdiParent = this; frm.Show(); |
This creates the instance of FrmAdoObjs and adds it as a MDI child to the main application window.
Select and drag a DBBrowserGrid from the Toolbox onto the form.
Select the DBBrowserGrid control. In the property grid for the control, change the following Grid Control properties:
Dock = Fill
BorderStyle = Fixed3D
Add the data access capabilities to the application: Copy the Northwind.mdb MS Access database file from the CD to the solution directory. Make sure that Microsoft JET 4.0 OLE DB provider is present on your computer.
Drag an OleDbDataAdapterComponent from toolbox to FrmAdoObjs. The Data Adapter Configuration Wizard appears. Configure the adapter for using the Microsoft JET 4.0 OLE DB provider and the Northwind.mdb database:
On a Data connection page, click New Connection.
The Data Link Properties dialog appears. Select the Provider tab, and then select the Microsoft JET 4.0 OLE DB provider.
Select the Connection tab and enter the path to the Northwind.mdb file:
Click OK to return to the wizard.
On the Generate the SQL Statements page of the wizard, click Advanced Options. Clear the Generate Insert, Update and Delete statements checkbox, and then click OK.
On the Generate the SQL Statements page, enter the "*" symbol as an SQL statement, and then click Finish. Disregard the error message, and click OK in the error message dialog box. You will setup the SQL statements later.
Drag a DataView component to FrmAdoObjs.
Double-click the FrmAdoObjs form. The code for
private void FrmAdoObjs_Load(object sender, System.EventArgs e) |
appears. Enter the following code as the function body:
oleDbConnection1.Open(); oleDbSelectCommand1.CommandText = "select * from customers"; oleDbDataAdapter1.Fill( this.dataSet1, "customers" ); oleDbSelectCommand1.CommandText = "select * from employees"; oleDbDataAdapter1.Fill( this.dataSet1, "employees" ); dataView1.Table = dataSet1.Tables["customers"]; dataView1.RowFilter = "City = 'London'"; dataView1.Sort = "Address"; |
The code will be executed on the Form. Load event and do the following: open the database connection, populate the dataset with two tables—"customers" and "employees"—and configure the data view for "customers" table.
Return to the FrmAdoObjs designer and double-click ADO.NET | DataSet, ADO.NET | DataTable, and ADO.NET | DataView on the main menu. The code for
void menuItem2_Click(object sender, System.EventArgs e) void menuItem3_Click(object sender, System.EventArgs e) void menuItem4_Click(object sender, System.EventArgs e) |
appears. Enter the following code as the function body:
private void menuItem2_Click(object sender, System.EventArgs e) { if( menuItem2.Checked ) { dbBrowserGrid1.SetDataBinding( null, null ); } else { dbBrowserGrid1.SetDataBinding( dataSet1, String.Empty ); } menuItem2.Checked = !menuItem2.Checked; menuItem3.Checked = false; menuItem4.Checked = false; } private void menuItem3_Click(object sender, System.EventArgs e) { if( menuItem3.Checked ) { dbBrowserGrid1.SetDataBinding( null, null ); } else { dbBrowserGrid1.SetDataBinding( dataSet1.Tables["employees"], null ); } menuItem3.Checked = !menuItem3.Checked; menuItem2.Checked = false; menuItem4.Checked = false; } private void menuItem4_Click(object sender, System.EventArgs e) { if( menuItem4.Checked ) { dbBrowserGrid1.SetDataBinding( null, null ); } else { dbBrowserGrid1.SetDataBinding( dataView1, null ); } menuItem4.Checked = !menuItem4.Checked; menuItem2.Checked = false; menuItem3.Checked = false; } |
Build and run the application. Create the MDI child window, click several times on the different items in the ADO.NET menu. You can see how different elements are bound and data from them are displayed in the DBBrowserGrid control. You can edit the records and see that changes are actually stored in the recordset. Changes are not submitted to the database because that task is beyond the scope of the data representation components.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.