Data Grids


Although the preceding sample relies heavily on .NET and VS.NET data features to reduce our coding, the list controls can take us only so far toward data-centric application nirvana. For example, the list box supports the display only of a single column from the underlying data set and provides no built-in UI for adding, editing, or deleting rows. For those features and a boatload of others, we've got the data grid .

The DataGrid control supports complex binding to list data sources. However, it does it a lot more thoroughly than do the list controls discussed so far. For one thing, a data grid can show all the columns , as shown in Figure 13.26.

Figure 13.26. Binding to a DataGrid

Binding a data source to the DataGrid control requires setting the DataSource and DataMember properties (again, watch out for setting more than just the data set in the DataSource property):

 
 Sub InitializeComponent()   ...   Me.dataGrid1.DataMember = "Customers"   Me.dataGrid1.DataSource = Me.customerSet1   ... End Sub 

The data grid supports showing not only multiple columns but also multiple tables. If you change your data set to include multiple tables and relationships between the tables, your grid will look like the one in Figure 13.27.

Figure 13.27. Showing 3-D Data in a Data Grid

When you click on the small plus sign on the grid, you can see links to all the relations, as shown in Figure 13.28.

Figure 13.28. Showing Relations

Clicking on the link brings you the related rows, shown in Figure 13.29.

Figure 13.29. Drilling through Relations

You can now see and navigate through the child rows of that relationship. As a convenience, the data grid also shows a history of the parent rows that you have navigated from.

Formatting Data Grids

In spite of the ability a data grid gives you to drill down into relations, the default layout of the data grid is not very useful. For example, we don't really want to show primary key identifiers to normal humans . The way to control the look and feel of the data grid is to use data grid table styles.

Data grid table styles (or just styles ) allow you to control which specific columns are shown, as well as column alignment, width, and header text and things like colors and fonts. Like most other things in .NET, styles are available via an object model, but the model is sufficiently complicated that we want to break with tradition and introduce you directly to DataGridTableStyle Collection Editor, available from the TableStyles property of the Property Browser. [6]

[6] If you feel the need to learn the data grid styles object model at the code level, I suggest starting with the code generated by the style editor, if for no other reason than it makes a wonderful data grid table styles macro editor.

Using the style editor, you'll want to create a set of styles for each data table in the grid, as shown in Figure 13.30.

Figure 13.30. DataGridTableStyle Collection Editor

For each table style, you set the MappingName property, choosing the name of the data table to which to apply the styles.

By default, a new table style has no columns at all. To add columns, bring up DataGridColumnStyle Collection Editor using the GridColumnStyle property for each table. The column style editor is shown in Figure 13.31 with a few columns added.

Figure 13.31. DataGridColumnStyle Collection Editor with Added Columns

Again, when you add a column, you specify the MappingName, this time choosing the column name. You'll probably also want to specify header text, or else the column will be blank. Width and alignment are other things you'll want to consider. Figure 13.32 shows an example of a data grid after some table and grid styles have been built.

Figure 13.32. A Stylish Data Grid

If you'd like something even fancier, you can format a data grid using the various color - and font-related properties on the data grid object itself. Or, if you don't want to spend the afternoon getting those settings just right, you can click the Auto Format link in the lower-right corner of the the Property Browser for a data grid. This opens the Auto Format dialog, where you can choose from an assortment of prefabricated formats, as shown in Figure 13.33.

Figure 13.33. The Data Grid Auto Format Dialog (See Plate 24)

Now, instead of spending the afternoon tweaking individual data grid properties, you can spend the afternoon choosing among the prepackaged sets of data grid properties. Or, if you'd like to return to the minimalist view, you can choose the Default format, which resets the data grid style properties to the defaults.

Data Exchange and Data Grids

Unlike the list controls, the data grid provides direct support for adding, updating, and deleting rows in the underlying data source. If you don't plan to replicate the data back to a data source (as discussed in Chapter 12: Data Sets and Designer Support), you may want to set the ReadOnly property of the data grid to true (it defaults to false). This will remove the capability to update the data.

Bringing It All Together

Figure 13.34 is an example of the kind of form you can produce using typed data sets, relations, expressions, the VS.NET data integration, master-detail, data binding, and data grids.

Figure 13.34. An Example of What WinForms Provides for Data Programmers (See Plate 25)

All the controls in this example bind to the same data source (customerSet1.Customers). Because they all use the same data source, the VCR navigation buttons (upper right) move the currency for all the data bound controls simultaneously . Also, this example uses two master-detail bindings to allow you to show the orders for the current customer and the order details for the current order. With data binding, the amount of code you will need to write to get this kind of core functionality working is minimal:

 
 Sub FullDataGridForm_Load(sender As Object, e As EventArgs)   sqlDataAdapter1.Fill(customerSet1) End Sub Sub button1_Click(sender As Object, e As EventArgs)   BindingContext(customerSet1, "Customers").Position = 0 End Sub Sub button2_Click(sender As Object, e As EventArgs)   BindingContext(customerSet1, "Customers").Position = _       BindingContext(customerSet1, "Customers").Position - 1 End Sub Sub button3_Click(sender As Object, e As EventArgs)   BindingContext(customerSet1, "Customers").Position = _       BindingContext(customerSet1, "Customers").Position + 1 End Sub Sub button4_Click(sender As Object, e As EventArgs)   BindingContext(customerSet1, "Customers").Position = _       BindingContext(customerSet1, "Customers").Count - 1 End Sub 

Of course, there's a whole lot more code behind Figure 13.34 than only these few lines, but the bulk of it is generated for you by VS.NET and provided by the WinForms data binding infrastructure. That, after all, is the whole point of data binding in the first place.



Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

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