Exposing the Data Table to the World

Team-Fly team-fly    

 
ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 11.   Creating Your Own Data Control


Let's compile the control. Open the form designer for frmCtlTest. If no connection or Select statement is set, then set them to Northwind and Select * From Customers, respectively. Now click on the DataGrid. Find the DataSource property. Drop down the list. What gives? The data control is not listed as a valid data source. What gives?

We have to implement an interface. What do we mean by implementing an interface? If you've designed ActiveX components in VB 6 you will have some idea. An interface is simply a predefined set of method signatures. This differs from a base class because the interface defines the methods. It cannot have any functionality and it cannot be inherited from. When we implement an interface, we are saying, "My class will contain these methods , and they have to be defined."

Okay, so which interface do we implement? IlistSource of the component model. This interface has two methods, GetList and ContainsListCollection. ContainsListCollection returns True if the component supplies a list. GetList returns the actual list object. Add the following code to the top of the class. The class declaration should look like this when you're finished:

 Public Class ucDataNav      Inherits System.Windows.Forms.UserControl      Implements System.ComponentModel.IListSource 

Notice there is a syntax error under System.Component-Model.Ilist-Source. Hover the mouse pointer over the code and see that one of the methods is not defined. Remember what we said about interfaces. When you implement an interface you are saying that your class will support all of the methods of the interface. So open the list of the components drop down at the top of the code window. You will see the interface listed. Select it. Now open the list of the procedure drop down. Click ContainsListCollection. A read-only property is created. Have the property return True.

 <System.ComponentModel.BrowsableAttribute(False)> _      Public ReadOnly Property ContainsListCollection() As Boolean _          Implements  System.ComponentModel.IListSource.ContainsListCollection          Get              ContainsListCollection = True          End Get      End Property 

Also add the attribute to prevent the property from showing in the Property window. Now do the same thing for the GetList method. But what do we want to return from this function? The DataTable object's DefaultView object is derived from the System.Collections.Ilist class, which is what GetList wants to return. Is my head sore!

 Public Function GetList() As System.Collections.IList Implements _               System.ComponentModel.IListSource.GetList         GetList = mDataSet.Tables(mintBoundTable).DefaultView  End Function 

Now compile the control and go back to the frmCtlTest designer. Look at the DataGrid's DataSource property. If you open the list, you will see the data navigator listed. Can we enable it to list the control and the tables in a tree view? Yes, we can. If you have a valid Connection and SQL Select statement assigned, when you select the DataSource the grid should populate its columns .


Team-Fly team-fly    
Top


ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net