Record Filtering in Visual Basic .NET

 <  Day Day Up  >  

Figure 9.2 shows the equivalent form in Visual Basic .NET.

Figure 9.2. A simple filtering form in Visual Basic .NET.
graphics/09fig02.jpg

I took the liberty of selecting one of the available styles for the grid. It's a nice compensation for only being able to adjust the individual column widths in code. (I expect that to be fixed in the next release of Visual Studio.)

Preparing the data for this example was a little more involved than it was in Visual FoxPro. First, I created a new Windows Forms application called SimpleFiltering, and added a SQLDataAdapter to the blank form. The resulting wizard asked for a connection, at which point I added a connection to the database that we've been using for our examples.

For the DataAdapter (which you could call daCountries , although I didn't change the default name of DataAdapter1 ), I specified the SQL statement SELECT Country FROM Countries . I also checked the Advanced Options button and unchecked the Generate Insert, Update and Delete Statements option because all we're doing is viewing the records.

For the Orders dataset, I dragged a second SQLDataAdapter onto the form, used SELECT OrderID, ShipName, ShipCity, ShipCountry FROM Orders as the SQL statement, unchecked the Generate Insert, Update and Delete Statements option, and specified a new dataset as shown in Figure 9.3.

Figure 9.3. Adding the dataset for the second data adapter.

graphics/09fig03.jpg


The Load event fills the datasets. Without this, you won't see a thing:

 

 SqlDataAdapter1.Fill(DsCountries1, "Countries") SqlDataAdapter2.Fill(DsOrders1, "Orders") 

The Grid's ReadOnly property is set to True . The ComboBox InteractiveChange event code is similar to that used in Visual FoxPro. However, note that the filtered records have to be explicitly moved to a DataView . I'm not sure whether FoxPro is doing something similar, or if Visual Basic .NET is simply applying a conditional index, as FoxPro does. It's a poorly kept secret that Visual Studio uses many elements of the FoxPro engine to manage datasets and data views:

 

 Dim dv As New DataView dv = DsOrders1.Tables(0).DefaultView dv.RowFilter = "ShipCountry='" + ComboBox1.Text + "'" DataGrid1.DataSource = dv 

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

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