Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1489

[VB6] Extending the DataGrid Control

$
0
0
Been playing with the DataGrid control to see if it could be usable in a project of mine. It's not a bad control, just a bit under-powered.

Caveat: The code and this posting is 99% focused on using the Data Grid without bound data. In other words, how to use the control without having a database to connect to.

This control is unicode aware and has a professional appearance. It can be very useful in many cases as a simple spreadsheet or to display subsets of databases. There are a few drawbacks to using this control.

1. It requires a bound list. Unlike its older counterpart, the DBGrid which could create its own internal collection of data, this control requires a data source. However, if you are connecting to an established DB, then not much to worry about. If you want to display your own custom data (no database), you can still do that via disconnected recordsets. They aren't that difficult, but you lose the advantage of SQL that could be used with a real DB. Here is a good example of using that other grid control: DBGrid, provided by dilettante

2. The multiselect feature is 'simple' vs. 'extended'. You can't use the shift key to select a range of rows

3. It has no OLE drag/drop feature.

4. The scrollbar does not respond to mouse wheels.

Most of those deficiencies can be overcome.

In the sample project provided, you will find a class: cGridEx. Lot's of comments provided too. This class extends the DataGrid control a bit. It does offer workarounds for extended mutliselection and dragging records from the control. I did not include code to drag stuff into the DataGrid; but you could do that with lots of elbow grease. The mouse wheel is not addressed in the cGridEx class since that workaround requires subclassing, but shown below one way to handle it.

As far as the bound data... The class has an option that can help generate an empty ADO recordset where you fill in the data and assign the recordset to the data grid. Populating a recordset is not rocket science -- use your favorite method. Remember that Excel & CSVs can be loaded into recordsets also (nearly automatically), allowing you to view them via a DataGrid. Also, recordsets can be persisted to file.

To handle the mouse wheel, the cGridEx class has a function to be called from subclassing. Post #5 has the sample subclass procedure

See post #5 for the updated version...

Viewing all articles
Browse latest Browse all 1489

Trending Articles