Quickly Bringing Data into a Worksheet

At times you may want to create a solution that displays data from a data source, but you have no intention of updating the data; perhaps you want to use the data binding capabilities of the list object as a way to get data in your document, for the purpose of viewing the data. Because ListObjects have two-way data binding that automatically updates data in the control, you must disconnect the host control from the data source by calling the Disconnect method on the ListObject.

You can add this code to the Startup event handler of the target worksheet. In our example, this is Sheet1 (see Listing 7.22). After you add this code and run the solution, you'll see that the list object contains the company data, but if you move your cursor through the rows of the list object, the NamedRange control that contains the contact information does not get updated. This is because the ListObject is no longer connected to the BindingSource.

Listing 7.22. Disconnecting a data source from a ListObject

Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles Me.Startup

'TODO: Delete this line of code to remove the 'default AutoFill for 'NorthwindDataSet.Customers'.

If Me.NeedsFill("NorthwindDataSet") Then Me.CustomersTableAdapter.Fill( _ Me.NorthwindDataSet.Customers)

End If

Me.CustomersListObject.Disconnect() End Sub

0 0

Post a comment