Binding to Other Objects (Arrays, Lists, etc.)

Team-Fly team-fly    

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

Table of Contents
Chapter 8.   Data Binding in Windows Forms


In addition to DataTable and DataViews, we can bind controls to simple arrays and list objects. A list object is any object that implements the IList interface. Besides the array, some other objects that implement the interface are the DataView, the ArrayList, and the CollectionBase (from which all collections are derived). So you see that we can bind controls to many more objects than we could in VB 6. You can also create your own bindable object just by implementing the interface yourself

To demonstrate this, let's create a new project, ADOBook08-02. Add controls to the default form to make it look like Figure 8.10.

Figure 8.10. Bound array form.

graphics/08fig10.gif

Binding to a single-dimension array is really simple. We cannot use any design-time methods to bind to an array because the array is not part of the component model. We must initialize the binding at runtime. This is a simple task. The following lines of code demonstrate the process:

 Private mList() As String      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button1.Click          Me.BindingContext(mList).Position += 1      End Sub      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button2.Click          Me.BindingContext(mList).Position -= 1      End Sub 
 Private Sub NumericUpDown1_ValueChanged(ByVal sender As Object,  ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged          Dim i As Integer          TextBox1.DataBindings.Clear()          ReDim mList(NumericUpDown1.Value)          For i = 0 To NumericUpDown1.Value              mList(i) = "Text" & i          Next          TextBox1.DataBindings.Add("Text", mList, "")      End Sub 

This is actually the entire program. This binds the text box to the array. The UpDown control allows us to vary the array size at runtime. As we click the Next and Previous buttons , the text box displays the values of the different elements of the array. Notice that when we create the binding, we supply an empty string to the property name parameter of the Add method. If we were binding to a DataTable, we would supply the column name here, but since this is a single-dimension array there is no property name to bind to. As far as I can tell, the documentation does not explicitly tell you this anywhere . I noticed that the parameter accepts an empty string and just tried it. If you try to use any other property of the string array, it fails. Any mention of this in the documentation would have saved me half a day of digging. But that's why you bought this book, isn't it?

It is not possible at this time to bind to a multidimensional array, at least not easily. One possibility would be to copy each column (Dimension 2) into a separate array and then bind to that, but the code to keep them in synch would get pretty complex and prone to errors. If you must bind to a two-dimensional array it would be better to use a DataTable object. Then you would be able to bind to the columns with no trouble.

In the object-oriented world, arrays are pass. The gods of OOP would rather you use collections in place of arrays. Collections, the OOP theory goes, are easier to manipulate, manage memory better, and are faster than arrays. I might buy into the first two arguments, but I find it hard to believe that they are faster. If you must use arrays, then do so, but be aware that they may disappear in some future revision. Java does not now natively support arrays, and even those in VB are pseudo-arrays; that is, they are really collections disguised as arrays. The only real arrays left can be found in C or C++ unmanaged code.


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