VB6 DataGrid OCX Control Alternative

Edited by

VB6 DataGrid control (MSDATGRD.OCX) problems

VB6 DataGrid OCX control shipped with Visual Basic (the msdatgrd.ocx file) does its main work, i.e. displays ADO Recordsets, enough good. But if you want to make its data rows more user-friendly (say, color cells based on some conditions), VB6 DataGrid control provides you with no helpful tools for that. We have a similar OCX grid control, iGrid, which can be used as an alternative to VB6 DataGrid control:

Sample form with VB6 DataGrid control (MSDatGrd.OCX) alternative

As you can see from this screenshot, iGrid OCX surpasses Visual Basic DataGrid in the following functionality:

  1. You can format cells with colors and fonts.

  2. You can sort DataGrid just clicking its column headers.

  3. DataGrid does not implement such cell editors as combobox and checkbox, but iGrid does.

  4. Cell text can be multiline and/or use other formatting options.

  5. iGrid has a very rich set of events to control the user's activity.

VB6 DataGrid Column Formatting

VB6 DataGrid control provides you with the ability to use the StdDataFormat object to format its columns dynamically - see, for instance, these articles:

VB6 Datagrid Column Formatting

HOW TO: Use Custom Formatting with a Visual Basic 6.0 Data Grid

If you used this approach, you already know that StdDataFormat is defined in a separate DLL, MSSTDFMT.DLL, but not in the DataGrid OCX file MSDATGRD.OCX. So you need to redistribute one more library with your end-user app and register it on client computers to make the whole system work.

Another restriction of this way of formatting is that it can be applied only to the whole column. iGrid has a similar tool (cell format strings), but it also gives you much more control over the individual cell or row look using such events as CellDynamicFormatting or RowDynamicFormatting. Here is an example in which we highlight cells with values greater than or equal to 50'000 with the red color in the Sum column:

Private Sub iGrid1_CellDynamicFormatting( _
     ByVal lRow As Long, ByVal lCol As Long, _
     ByVal vValue As Variant, _
     oForeColor As Long, oBackColor As Long, _
     oFont As stdole.StdFont)
   If iGrid1.ColKey(lCol) = "sum" Then
      If vValue >= 50000 Then
         oForeColor = vbRed
      End If
   End If
End Sub

As you can see from the event signature, you can change text color, background color and even font for any cell or the whole row in one event handler on-the-fly.

Unbound DataGrid control

Visual Basic DataGrid allows you to upload data from a database table or query, but in many cases you also need to display data stored not in a database (in a CSV file, an array, etc). In other words, you need an unbound DataGrid control. If you want to display these data in the DataGrid from MSDatGrd.ocx, first you need to load them into a temporary interim data store and only after that bind your DataGrid to it to show your data on the screen.

But this is just a half of the problem - you cannot freely change or format your data in individual cells as they are in fact the underlying recordset fields. And this is really annoying if you need data of different data types and/or formatting in the same column.

iGrid is free from all the drawbacks mentioned above. Unbound mode, when you can change its cell independently, is its main work mode - though it also allows you to upload ADO and DAO Recordsets with one command and use all the features with them without any restrictions.

Versatile data binding

Despite the fact that iGrid works mainly in unbound mode, you can make it a data-bound control by adding some code. A big benefit of this approach is the ability to control all aspects of the functionality - you will never get such a level of control in DataGrid.

The main demo of iGrid contains a series of samples which demonstrate different approaches of data binding you can implement with iGrid. You can cache the user changes on the field-, row- or entire grid-level, or you can store your edited data in an ADODB Recordset or retrieve and update them directly through an ADODB Connection object. This is ready-to-use code you can use in your apps for free as templates, and (what is most important!) change the functionality in every particular scenario to fully suit your users' needs.

Add to this the modern look-n-feel based on the visual styles from the latest versions of the Windows OS, such as Windows XP, Vista and Windows 7, and you get an excellent replacement for VB6 DataGrid OCX Control!

Discover more advantages of the iGrid OCX grid control »