Dela via


Walkthrough: Simple data binding in a document-level project

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This walkthrough demonstrates the basics of data binding in a document-level project. A single data field in a SQL Server database is bound to a named range in Microsoft Office Excel. The walkthrough also shows how to add controls that enable you to scroll through all the records in the table.

Applies to: The information in this topic applies to document-level projects for Excel. For more information, see Features available by Office application and project type.

This walkthrough illustrates the following tasks:

  • Creating a data source for an Excel project.

  • Adding controls to a worksheet.

  • Scrolling through database records.

    Note

    Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.

Prerequisites

You need the following components to complete this walkthrough:

  • An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configure a computer to develop Office solutions.

  • Excel 2013 or Excel 2010.

  • Access to a server with the Northwind SQL Server sample database.

  • Permissions to read from and write to the SQL Server database.

Create a new project

In this step, you will create an Excel workbook project.

To create a new project

  1. Create an Excel workbook project with the name My Simple Data Binding, using either Visual Basic or C#. Make sure that Create a new document is selected. For more information, see How to: Create Office projects in Visual Studio.

    Visual Studio opens the new Excel workbook in the designer and adds the My Simple Data Binding project to Solution Explorer.

Create the data source

Use the Data Sources window to add a typed dataset to your project.

To create the data source

  1. If the Data Sources window is not visible, display it by, on the menu bar, choosing View > Other Windows > Data Sources.

  2. Choose Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database and then click Next.

  4. Select a data connection to the Northwind sample SQL Server database, or add a new connection using the New Connection button.

  5. After a connection has been selected or created, click Next.

  6. Clear the option to save the connection if it is selected, and then click Next.

  7. Expand the Tables node in the Database objects window.

  8. Select the check box next to the Customers table.

  9. Click Finish.

    The wizard adds the Customers table to the Data Sources window. It also adds a typed dataset to your project that is visible in Solution Explorer.

Add controls to the worksheet

For this walkthrough, you need two named ranges and four buttons on the first worksheet. First, add the two named ranges from the Data Sources window so that they are automatically bound to the data source. Next, add the buttons from the Toolbox.

To add two named ranges

  1. Verify that the My Simple Data Binding.xlsx workbook is open in the Visual Studio designer, with Sheet1 displayed.

  2. Open the Data Sources window and expand the Customers node.

  3. Select the CompanyName column, and then click the drop-down arrow that appears.

  4. Select NamedRange in the drop-down list, and then drag the CompanyName column to cell A1.

    A NamedRange control named companyNameNamedRange is created in cell A1. At the same time, a BindingSource named customersBindingSource, a table adapter, and a DataSet instance are added to the project. The control is bound to the BindingSource, which in turn is bound to the DataSet instance.

  5. Select the CustomerID column in the Data Sources window, and then click the drop-down arrow that appears.

  6. Click NamedRange in the drop-down list, and then drag the CustomerID column to cell B1.

  7. Another NamedRange control named customerIDNamedRange is created in cell B1, and bound to the BindingSource.

To add four buttons

  1. From the Common Controls tab of the Toolbox, add a Button control to cell A3 of the worksheet.

    This button is named Button1.

  2. Add three more buttons to the following cells in this order, so that the names are as shown:

    Cell (Name)
    B3 Button2
    C3 Button3
    D3 Button4

    The next step is to add text to the buttons, and in C# add event handlers.

Initialize the controls

Set the button text and add event handlers during the Startup event.

To initialize the controls

  1. In Solution Explorer, right-click Sheet1.vb or Sheet1.cs, and then click View Code on the shortcut menu.

  2. Add the following code to the Sheet1_Startup method to set the text for each button.

    this.button1.Text = "|<";
    this.button2.Text = "<";
    this.button3.Text = ">";
    this.button4.Text = ">|";
    
    With Me
        .Button1.Text = "|<"
        .Button2.Text = "<"
        .Button3.Text = ">"
        .Button4.Text = ">|"
    End With
    
  3. For C# only, add event handlers for the button click events to the Sheet1_Startup method.

    this.button1.Click += new EventHandler(button1_Click);
    this.button2.Click += new EventHandler(button2_Click);
    this.button3.Click += new EventHandler(button3_Click);
    this.button4.Click += new EventHandler(button4_Click);
    

    Now add code to handle the Click events of the buttons so that the user can browse through the records.

Add code to enable scrolling through the records

Add code to the Click event handler of each button to move through the records.

To move to the first record

  1. Add an event handler for the Click event of the Button1 button, and add the following code to move to the first record:

    private void button1_Click(object sender, System.EventArgs e)
    {
        this.customersBindingSource.MoveFirst();
    }
    
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
    
        Me.CustomersBindingSource.MoveFirst()
    End Sub
    

To move to the previous record

  1. Add an event handler for the Click event of the Button2 button, and add the following code to move the position back by one:

    private void button2_Click(object sender, System.EventArgs e)
    {
        this.customersBindingSource.MovePrevious(); 
    }
    
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Button2.Click
    
        Me.CustomersBindingSource.MovePrevious()
    End Sub
    

To move to the next record

  1. Add an event handler for the Click event of the Button3 button, and add the following code to advance the position by one:

    private void button3_Click(object sender, System.EventArgs e)
    {
        this.customersBindingSource.MoveNext();
    }
    
    Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Button3.Click
    
        Me.CustomersBindingSource.MoveNext()
    End Sub
    

To move to the last record

  1. Add an event handler for the Click event of the Button4 button, and add the following code to move to the last record:

    private void button4_Click(object sender, System.EventArgs e)
    {
        this.customersBindingSource.MoveLast();
    }
    
    Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Button4.Click
    
        Me.CustomersBindingSource.MoveLast()
    End Sub
    

Test the application

Now you can test your workbook to make sure that you can browse through the records in the database.

To test your workbook

  1. Press F5 to run your project.

  2. Confirm that the first record appears in cells A1 and B1.

  3. Click the > (Button3) button and confirm that the next record appears in cell A1 and B1.

  4. Click the other scroll buttons to confirm that the record changes as expected.

Next steps

This walkthrough shows the basics of binding a named range to a field in a database. Here are some tasks that might come next:

See also