Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid, StdDataFormat & TargetObject

Status
Not open for further replies.

ronmoore

Programmer
May 26, 2001
2
US
I have a working VB6 program with a Datagrid bound to an ADO Recordset. I am using the Format and UnFormat events of a StdDataFormat object to apply special formatting to the Columns of the Datagrid, leaving the values in the database unformatted. So far, so good.

My problem is that I want to be able to determine, from within the Format/UnFormat event subroutines, which column of the Datagrid is being processed (there is only one StdDataFormat object shared by all columns that need the special formatting). The Format/Unformat subroutines have a single parameter, the DataValue object. The DataValue object (a StdFormat object) has two properties, Value and TargetObject. The Value works fine, but the TargetObject, which should give me exactly what I am looking for, is set to Nothing, when I would have expected that it would be set to a Datagrid.Column object.

I stress that the StdDataFormat Format/UnFormat mechanism is working correctly, except that TargetObject is Nothing, preventing me from discriminating between the Columns of the Datagrid.

As an experiment, I wrote a similar program using a Textbox in place of a Datagrid, and in this case the TargetObject was set to the Textbox as expected.

Does anyone out there have experience with using TargetObject with a Datagrid? Is it possible that the TargetObject is not being set because the Datagrid requires complex binding (a field is bound to each column)?
 
Follow-up to original submission.

Here is a small program which demonstrates the problem.

It needs a reference to the Microsoft Data Formatting Library (MSSTDFMT.DLL)

Also uses the component Microsoft Datagrid Control (MSDATAGRD.OCX)


Option Explicit

' This program demonstrates a problem with the TargetObject property of the StdDataValue object.
' The recordset rs contains the values 0 (False) and -1 (True).
' The Format/UnFormat event subroutines cause the display to be "False" and "True".
' In the case of the Textbox, the TargetObject is available and is used to set the BackColor of the Textbox.
' In the case of the Datagrid, the TargetObject is Nothing.

Private rs As ADODB.Recordset
Private WithEvents fmtBoolean As StdDataFormat ' Formatting Object for Datagrid
Private WithEvents fmtText1 As StdDataFormat ' Formatting Object for Textbox

Private Sub fmtBoolean_Format(ByVal DataValue As StdFormat.StdDataValue)
If DataValue.TargetObject Is Nothing Then
Debug.Print "fmtBoolean_Format:TargetObject is Nothing!"
End If

' DataValue.TargetObject is Nothing here!
' The statement below operates directly on Datagrid1.Columns(0).
' It DOES work, but causes this Sub to be called repetitively.
' If DataValue.TargetObject could be used in place of Datagrid1.columns(0), perhaps this looping would be avoided.

' DataGrid1.Columns(0).Alignment = IIf(DataValue.Value, dbgRight, dbgLeft) ' align False left, True right

DataValue.Value = IIf(DataValue.Value, "True", "False")
End Sub

Private Sub fmtBoolean_UnFormat(ByVal DataValue As StdFormat.StdDataValue)
DataValue.Value = (UCase(DataValue.Value) = "TRUE")
End Sub

Private Sub fmtText1_Format(ByVal DataValue As StdFormat.StdDataValue)
DataValue.Value = IIf(DataValue.Value, "True", "False")
DataValue.TargetObject.BackColor = IIf(DataValue.Value, vbGreen, vbRed)
End Sub

Private Sub fmtText1_UnFormat(ByVal DataValue As StdFormat.StdDataValue)
DataValue.Value = (UCase(DataValue.Value) = "TRUE")
End Sub

Private Sub Form_Load()
Dim b As Boolean
Dim i As Integer

Set rs = New ADODB.Recordset
rs.Fields.Append "Boolean", adBoolean
rs.Open ' connectionless recordset with single Boolean field

DataGrid1.TabAction = dbgGridNavigation
DataGrid1.WrapCellPointer = True
Set DataGrid1.DataSource = rs ' bind recordset to Datagrid

Set Text1.DataSource = rs ' bind recordset to Textbox Text1 (shows False/True)
Text1.DataField = "Boolean"

Set Text2.DataSource = rs ' bind recordset to Textbox Text2 (shows 0, -1)
Text2.DataField = "Boolean"

' set up Text1 Formatting
Set fmtText1 = New StdDataFormat
Set Text1.DataFormat = fmtText1

' set up Datagrid Formatting
Set fmtBoolean = New StdDataFormat
Set DataGrid1.Columns(0).DataFormat = fmtBoolean

For i = 0 To 5 ' create 6 records in connectionless recordset for testing
rs.AddNew
rs!Boolean = b ' alternates False/True
b = Not b
Next i
rs.MoveFirst

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top