Home

ADO.NET Visual Support: The Binding Navigator

     

Introduction

If you drag a table from the Data Source window and drop it on a form, Microsoft Visual Studio adds the necessary Windows controls to the form and binds them to the columns of the table. To move from one record to another, you would need a way to navigate among the records. You can manually take care of this if you want. Alternatively, the .NET Framework provides a class named BindingNavigator that contains all the necessary functionality for this task.

   

Practical LearningPractical Learning: Introducing Visual Database Support

  1. Start Microsoft Visual Studio
  2. Create a Windows Forms Application named spr1
  3. In the Solution Explorer, right-click Form1.cs and click Rename
  4. Type RentalManagement.cs and press Enter
  5. Right-clkick the form and click Properties
  6. Click Text and type Solas Property Rental - Properties Listing
  7. Double-click the middle of the form and implement the Load event as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace spr1
    {
        public partial class RentalManagement : Form
        {
            public RentalManagement()
            {
                InitializeComponent();
            }
    
            private void CreateRentalDatabase()
            {
                // We will use a directory named Solas Property Rental on the C: drive.
                // This will make it easy to delete the database and its files
                // when we don't need them anymore
                // If that directory was not yet created, create it
                Directory.CreateDirectory(@"C:\Solas Property Rental");
    
                using (SqlConnection cnnSolasPropertyRental =
                    new SqlConnection("Data Source=(local);" +
                                      "Integrated Security='SSPI';"))
                {
                    string strSolasPropertyRental =
                        "CREATE DATABASE SolasPropertyRental1 " +
                        "ON PRIMARY (NAME = RentalRecords, FILENAME = 'C:\\Solas Property Rental\\SolasPropertyRental1.mdf') " +
                        "LOG ON (NAME = RentalLog, FILENAME = 'C:\\Solas Property Rental\\SolasPropertyRental1.ldf')";
    
                    SqlCommand cmdSolasPropertyRental =
                        new SqlCommand(strSolasPropertyRental,
                            cnnSolasPropertyRental);
    
                    cnnSolasPropertyRental.Open();
                    cmdSolasPropertyRental.ExecuteNonQuery();
    
                    MessageBox.Show("The SolasPropertyRental1 database has been created",
                                    "Solas Property Rental",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                using (SqlConnection cnnSolasPropertyRental =
                new SqlConnection("Data Source=(local);" +
                    "Database='SolasPropertyRental1';" +
                    "Integrated Security='SSPI';"))
                {
                    string strSolasPropertyRental =
                        "CREATE TABLE Tenants " +
                        "( " +
                        " 	TenantID int identity(1,1) NOT , " +
                        " 	TenantCode nchar(16) , " +
                        " 	FullName nvarchar(50) , " +
                        " 	MaritalStatus nvarchar(40), " +
                        " 	ContactNumber nvarchar(20) " +
                        ");";
    
                    SqlCommand cmdSolasPropertyRental =
    		    new SqlCommand(strSolasPropertyRental, cnnSolasPropertyRental);
    
                    cnnSolasPropertyRental.Open();
                    cmdSolasPropertyRental.ExecuteNonQuery();
    
                    MessageBox.Show("The Tenants table has been created",
                                    "Solas Property Rental",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                using (SqlConnection cnnSolasPropertyRental =
                new SqlConnection("Data Source=(local);" +
                    "Database='SolasPropertyRental1';" +
                    "Integrated Security='SSPI';"))
                {
                    string strSolasPropertyRental =
                        "CREATE TABLE RentalProperties " +
                        "( " +
                        " 	RentalPropertyID int identity(1,1), " +
                        " 	PropertyCode nchar(16) , " +
                        " 	PropertyType nvarchar(32) , " +
                        "	Address nvarchar(50), " +
                        "	City  nvarchar(40), " +
                        "	State nchar(2), " +
                        "	ZIPCode nvarchar(12), " +
                        " 	Bedrooms tinyint, " +
                        " 	Bathrooms float, " +
                        "	HasCarpet bit, " +
                        "	HardWoodFloor bit, " +
                        "	IndoorGarage bit, " +
                        "	HasWasherDryer bit, " +
                        "	PetsAllowed bit, " +
                        " 	OccupancyStatus nvarchar(30)', " +
                        " 	MonthlyRent money);";
    
                    SqlCommand cmdSolasPropertyRental =
    		    new SqlCommand(strSolasPropertyRental, cnnSolasPropertyRental);
    
                    cnnSolasPropertyRental.Open();
                    cmdSolasPropertyRental.ExecuteNonQuery();
    
                    MessageBox.Show("The RentalProperties table has been created",
                                    "Solas Property Rental",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                using (SqlConnection cnnSolasPropertyRental =
                new SqlConnection("Data Source=(local);" +
                    "Database='SolasPropertyRental1';" +
                    "Integrated Security='SSPI';"))
                {
                    string strSolasPropertyRental =
                        "CREATE TABLE RentalAllocations " +
                        "( " +
                        " 	AllocationID int identity(1,1), " +
                        " 	AllocationCode nchar(16) , " +
                        " 	DateAllocated date, " +
                        " 	PropertyCode nchar(16) , " +
                        " 	TenantCode nchar(16) , " +
                        "   ContractLength nvarchar(50) , " +
                        " 	RentStartDate date, " +
                        " 	MonthlyRent money);";
    
                    SqlCommand cmdSolasPropertyRental =
    		    new SqlCommand(strSolasPropertyRental, cnnSolasPropertyRental);
    
                    cnnSolasPropertyRental.Open();
                    cmdSolasPropertyRental.ExecuteNonQuery();
    
                    MessageBox.Show("The RentalAllocations table has been created",
                                    "Solas Property Rental",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                using (SqlConnection cnnSolasPropertyRental =
                new SqlConnection("Data Source=(local);" +
                    "Database='SolasPropertyRental1';" +
                    "Integrated Security='SSPI';"))
                {
                    string strSolasPropertyRental =
                        "CREATE TABLE RentPayments " +
                        "( " +
                        " 	PaymentID int identity(1,1), " +
                        " 	ReceiptNumber nchar(16) , " +
                        " 	PaymentDate date, " +
                        " 	AllocationCode nchar(16) , " +
                        "   PaymentForMonth nvarchar(20), " +
                        "   PaymentForYear int, " +
                        " 	Amount money);";
    
                    SqlCommand cmdSolasPropertyRental =
    		    new SqlCommand(strSolasPropertyRental, cnnSolasPropertyRental);
    
                    cnnSolasPropertyRental.Open();
                    cmdSolasPropertyRental.ExecuteNonQuery();
    
                    MessageBox.Show("The RentPayments table has been created",
                                    "Solas Property Rental",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
    
            private void RentalManagement_Load(object sender, EventArgs e)
            {
                CreateRentalDatabase();
            }
        }
    }
  8. Press F5 to execute the application
  9. Click OK as many times as necessary
  10. Close the form and return to your programming environment
  11. Access the source code of the form and change it as follows:
    private void RentalManagement_Load(object sender, EventArgs e)
    {
        // CreateRentalDatabase();
    }
    
  12. Display the form
  13. On the main menu, click Data -> Add New Data Source...
  14. On the first page of the wizard, make sure Database is selected and click Next
  15. In the second page of the wizard, make sure Dataset is selected and click Next
  16. In the third page of the wizard, click New Connection...
  17. In the Server Name combo box, select the server or type (local)
  18. In the Select or Enter a Database Name combo box, select SolasPropertyRental1
  19. Click Test Connection
     
    Add Connection
  20. Click OK twice
  21. On the Data Source Configuration Wizard, make sure the new connection is selected
    Click the + button of Connection String
     
    Data Source Configuration Wizard
  22. Click Next
  23. Change the connection string to csSolasPropertyRental and click Next
  24. Expand the Tables node and click the check box of RentalProperties
  25. Change the name of the data set to dsSolasPropertyRental
     
    Data Source Configuration Wizard
  26. Click Finish
  27. Display the form if ncessary.
    On the Toolbox, click Data
  28. Click BindingSource
  29. Click the form
  30. In the Properties window, change its (Name) to bsSolasPropertyRental
  31. Click DataSource and click the arrow its combo box
  32. Click the + button of the Other Data Sources node to expand it
  33. Click the + button of the Project Data Sources node
  34. Click dsSolasPropertyRental
  35. While the binding source is still selected under the form, in the Properties window, click DataMember and click the arrow its combo box to select RentalProperties
  36. Under the form, click rentalPropertiesTableAdapter
  37. In the Properties window, click (Name) and type taRentalProperties

Creating a Binding Navigator

There are various ways you can create a binding navigator:

  • You can declare a variable of type BindingNavigator and configure it
  • From the Data section of the Toolbox, you can drag a BindingNavigator object and drop it on a form. You should then access the Properties window for the binding navigator. To support this, the BindingNavigator class has a property named BindingSource. You can access it from the Properties window of the binding navigator and select the binding source
  • If you drag a table from the Data Source window and drop it on a form, Microsoft Visual Studio would create and configure a binding navigator for you

Practical LearningPractical Learning: Creating a Binding Navigator

  1. In the Data section of the Toolbox, click BindingNavigator
  2. Click the form
  3. In the Properties window, change its Name to bnRentalProperties
  4. Still in the Properties window, click BindingSource and select bsSolasPropertyRental
  5. In the Data section of the Toolbox, click DataGridView
  6. Click the form
  7. Still in the Properties window, click DataSource and select bsSolasPropertyRental
  8. Under the Properties window, click Edit Columns and configure the columns as follows:
     
    Column HeaderText Width
    RentalPropertyID Prop ID 50
    PropertyCode Prop Code 70
    PropertyType Property Type 90
    Bedrooms Beds 50
    Bathrooms Baths 50
    MonthlyRent Monthly Rent 80
    OccupancyStatus Status 90
  9. Click OK
  10. In the Properties window, change the following characteristics:
    (Name): dgvRentalProperties
    Anchor: Top, Bottom, Left, Right
    ColumnHeadersHeightSizeMode: EnableResizing
     
    Solas Property Rental
  11. Execute the application to test it
     
    Solas Property Rental
  12. Close the form and return to your programming environment
  13. In the Server Explorer, right-click the SolasPropertyRental1 connection and click Delete
 

Home Copyright © 2010-2016, FunctionX