View Single Post
Old 07-28-2006, 12:16 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,400
Default VB Grids

"Old MacDonald had a Grid! E - I - E - I - Ohhh..."

Wait... Those aren't the right words. Oh well - close enough for this tutorial.

We're here to talk about grids. In particular, the grids that Microsoft supplies with VB.

If you are reading this, you probably know what a grid is; a control used to display tabular data that looks like a spreadsheet. And, the dbGrid looks like a datagrid, which looks like a flexgrid... So, they must be interchangeable, right??? Wrong. As programmers, we need to know which tools are available for our use, their strengths and weaknesses, and under what circumstances we should use (or, sometimes more importantly, not use) a given tool.

OK. Let's talk about the native grids that we have available to use: the dbGrid, the DataGrid, the FlexGrid, and the mshFlexGrid.
  • dbGrid: The dbGrid is the earliest grid control available for use with VB. It can be used either bound or un-bound. By this, I mean that you may bind the dbGrid to a DAO recordset, or not. If it is bound, it may be used to both display the data from the recordset, AND it may be used to edit the data within the recordset.
  • DataGrid: The DataGrid may only be used bound to an ADO recordset. Because of this it is less flexible than the flexgrid, but, on the other hand, it is also considerably faster than a flexgrid, which may be important if you are working with large recordsets.
  • FlexGrid: The Flexgrid may be used either bound or unbound. It may be bound to a DAO recordset. It is a read-only control. If unbound, you may write data directly to the grid, or read data from the grid.
  • mshFlexGrid: The Hierarchal FlexGrid is very similar to the Flexgrid; however, it must be bound to an ADO recordset, or used unbound. Also like the Flexgrid, it is a 'read-only' grid. It also supports the heirarchial display of data, where common fields can be "joined" for display. It supports ADO shaped recordsets.

The dbGrid and DataGrid are the simpler, less flexible grid controls. While they work well for simple grid applications, they are much less flexible than are the two versions of flexgrid. Offsetting this is the fact that, since they DO have less overhead, they are more efficient than their more complex flexgrid relatives, and thus can more quickly display large recordsets.

Of the four native grid types provided by Microsoft for VB6, only the DataGrid MUST be bound to a recordset; the other three all support direct insertion of data by your application. A DataGrid, while it must be bound to an ADO recordset, does not need to be tied to a database however, as the recordset may be created programmatically as a stand-alone recordset. Both varieties of FlexGrid are read-only. In order to edit data in the grid, you must employ techniques like opening a sub-form, or entering data into a textbox, and then updating the underlying grid. (Or, if the grid is bound, updating the underlying recordset and refreshing the grid.) Also note, that even though flexgrids are read-only, techniques do exist to emulate direct edit capabilities.

In closing, I mentioned earlier that the dbGrid and DataGrid may be better suited for displaying large recordsets. While this is true, IF you find that you need to display large recordsets, you should really think about redesigning your application. Large recordset displays should rarely, if ever be employed. The idea of scrolling through thousands of records is NOT a good GUI design. It is tedious and error-prone for the end user. Plus, holding many thousands of records is VERY memory intensive on the client, and bandwidth intensive on the network itself. Generally, better alternatives exist. (Setting up paging, filters, or searches, to limit the recordset and displayed data, for instance.)

Finally, any post dealing with grids would not be complete without mentioning (and linking to) Flyguy's Flexgrid Functions. Flyguy has written a huge tutorial/code base in our Code Library forum, and he provides sample code and explanation on many different aspects of working with both the FlexGrid and the mshFlexgrid controls. In addition, and on a much smaller scale, I've created a thread that discusses printing and formatting the datagrid.

I've also attached a PDF and RTF table summarizing VB6 grid properties.
Attached Files
File Type: pdf VB6 Native Grids_Grid.pdf (50.6 KB, 228 views)
File Type: rtf VB6 Native Grids_Grid.rtf (11.3 KB, 103 views)
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown

Last edited by loquin; 05-01-2007 at 11:58 PM.
Reply With Quote