微信软件工作室 (MicroInfo Soft Studio) 
http://www.playyuer.com
于溪玥(PlayYuer) 2000/02/01 帖:
《VB6 之数据格式化对象使用技巧》(http://microinfo.top263.net/Txt/Format.txt
)
例程下载:(http://microinfo.top263.net/Zip/Format.exe)
Visual Basic 提供了许多工具以满足数据访问编程的需要,其中引用微软数据格式化对象库
(Microsoft Data Formatting Object Library) 格式化对象技术是 VB6 的增强功能。其最大优
点是可以通过编程随心所欲地格式化数据(可参阅 MSDN )。下面以格式化 DataGrid 各列数据为
例,演示数据格式化对象的使用方法。首先,根据 DataGrid 各列的数据类型,确定格式化后的格式:
      布尔(Boolean)型数据:True = 是;False = 否;
      数字(Number)型数据:格式化为大写;
其次,确认工程已经引用(Reference)了Microsoft Data Formatting Object Library。
再次,在窗体上绘制一个DataGrid控件。
接下来,就可以编写代码了:1.声明带 WithEvents 关键字的 StdDataFormat 对象类型的窗体模块级的私有变量,作为大写
  格式的数据格式对象:  Option Explicit
  Private WithEvents FmtX As StdDataFormat
2.编写Private Sub Form_Load()事件过程:  Private Sub Form_Load()
  '创建含有货币、布尔、数字型字段的记录集
  Dim adoRecordset As New ADODB.Recordset
  adoRecordset.Fields.Append "Fld1", adCurrency, , adFldIsNullable + adFldMayBeNull
  adoRecordset.Fields.Append "Fld2", adBoolean, , adFldIsNullable + adFldMayBeNull
  adoRecordset.Fields.Append "Fld3", adDouble, , adFldIsNullable + adFldMayBeNull
  adoRecordset.Open
  adoRecordset.AddNew Array("fld1", "fld2", "fld3"), Array(1001231.01, True, 634532.23)
  adoRecordset.AddNew Array("fld1", "fld2", "fld3"), Array(1000.23, False, 89758242)  Set DataGrid1.DataSource = adoRecordset  FormatDataGrid DataGrid1
  End Sub
3.编写一个过程,格式化 DataGrid,可在需要的时机调用:  Sub FormatDataGrid(DataGridX As DataGrid)
      Dim FmtY As New StdDataFormat '用于布尔型字段格式
      FmtY.Type = fmtBoolean
      FmtY.TrueValue = "是" '"真" "对"
      FmtY.FalseValue = "否" '"假" "错"      Set FmtX = New StdDataFormat '实例化用于格式化货币型字段的对象
      FmtX.Type = fmtCustom '用于货币型字段格式      Dim adoRecordsetX As ADODB.Recordset
      Set adoRecordsetX = DataGridX.DataSource      Dim i As Integer
      For i = 0 To adoRecordsetX.Fields.Count - 1
          With DataGridX.Columns.Item(i)
                Select Case adoRecordsetX.Fields.Item(i).Type  '可根据字段数据类型设置数据格式
                      Case adNumeric, adVarNumeric, _
                            adBigInt, adUnsignedInt, adUnsignedTinyInt, _
                            adUnsignedSmallInt, adUnsignedBigInt, _
                            adInteger, adSmallInt, adBigInt, adTinyInt, _
                            adDecimal, adDouble, adSingle '与数字有关的数据类型
                            .DataFormat.Format = "Standard"
                            .Alignment = dbgRight
                      Case adCurrency '货币型
                            Set .DataFormat = FmtX
                            .Alignment = dbgRight
                      Case adBoolean
                            Set .DataFormat = FmtY '布尔型字段设为自定义格式
                            .Alignment = dbgCenter
                      Case adDate, adDBDate, adDBTimeStamp
                            .DataFormat.Format = "Long Date" '日期、时间
                            .Alignment = dbgRight
                      Case Else '其它,如:文本等
                            .Alignment = dbgLeft
                    End Select
          End With
      Next i
  End Sub
4.编写数据格式化对象的格式化 (Format) 事件过程:  Private Sub FmtX_Format(ByVal DataValue As StdFormat.StdDataValue)
  '读者可自行编写将数字转化为大写的函数!例如(附录: NtoC 函数):
  DataValue = NtoC(Trim(Str(DataValue)), "圆", "角", "分")
  End Sub  (该方案在 PWin 9.X 和 VB 6.0 下通过。)
    数据格式化对象,可以用于格式化各种具有 DataFormat 属性的控件,程序员可以将数据显示
为各种格式。如果格式非常简单,可以使用传统的 Format 函数。如果格式布尔型数据,可以使用
过程级的数据格式化对象。如果格式非常复杂,情况很多,则须使用窗体模块级的带事件数据格式
化对象,并编程该对象的 Format 等事件。(欢迎去 Http://microinfo.top263.net 下载例程)================================================
《 附录: NtoC 函数 》
Option Explicit
Public Function NtoC(ByVal sNum As String, Optional ByVal Yuan As String = "美圆", Optional ByVal Jiao As String = "美角", Optional ByVal Fen As String = "美分") As String
If Val(Trim(sNum)) > 0 Then
  Dim sIntD, sDecD As String
  Dim i, iCount, j, iLength As Integer
  Dim lStartPos As Long
  Dim sBIT(4), sUNIT(3), sCents(2) As String
  sBIT(0) = "" '个
  sBIT(1) = "拾"
  sBIT(2) = "佰"
  sBIT(3) = "仟"
  sUNIT(0) = ""
  sUNIT(1) = "万"
  sUNIT(2) = "亿"
  sUNIT(3) = "yu"
  sCents(0) = Fen
  sCents(1) = Jiao
  Dim temp As String
  If InStr(Trim(sNum), ".") > 0 Then
      temp = Left(Trim(sNum), InStr(Trim(sNum), ".") - 1)
  Else
      temp = Trim(sNum)
  End If
  iCount = IIf(Len(temp) Mod 4, Len(Trim(temp)) \ 4 + 1, Len(Trim(temp)) \ 4)
  lStartPos = 1
  For i = iCount To 1 Step -1
      If i = iCount And Len(Trim(temp)) Mod 4 <> 0 Then
          iLength = Len(Trim(temp)) Mod 4
      Else
          iLength = 4
      End If
      sIntD = Mid(Trim(temp), lStartPos, iLength)
      For j = 1 To Len(Trim(sIntD))
          If Val(Mid(sIntD, j, 1)) <> 0 Then
              NtoC = NtoC & Choose(Val(Mid(sIntD, j, 1)), "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖") & sBIT(Len(Trim(sIntD)) - j)
          Else
              If Val(Mid(sIntD, j + 1, 1)) <> 0 Then
                NtoC = NtoC & "零"
              End If
          End If
      Next j
      lStartPos = lStartPos + iLength
      If i < iCount Then
          If (Val(Mid(sIntD, Len(Trim(sIntD)), 1)) <> 0 Or Val(Mid(sIntD, Len(Trim(sIntD)) - 1, 1)) <> 0 Or Val(Mid(sIntD, Len(Trim(sIntD)) - 2, 1)) Or Val(Mid(sIntD, Len(Trim(sIntD)) - 3, 1)) <> 0) Then
            NtoC = NtoC & sUNIT(i - 1)
          End If
      Else
          NtoC = NtoC & sUNIT(i - 1)
      End If
  Next
  If Len(Trim(NtoC)) > 0 Then
      NtoC = NtoC & Yuan
  End If
  '小数
  If InStr(1, Trim(sNum), ".") <> 0 Then
      sDecD = Right(sNum, Len(Trim(sNum)) - InStr(1, Trim(sNum), "."))
      For i = 1 To Len(Trim(sDecD))
          If Val(Mid(Trim(sDecD), i, 1)) <> 0 Then
            NtoC = NtoC & Choose(Val(Mid(Trim(sDecD), i, 1)), "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
            NtoC = NtoC & sCents(2 - i)
            If i >= 2 Then
                Exit For
            End If
          Else
            If Len(Trim(NtoC)) > 0 Then
                NtoC = NtoC & "零"
            End If
          End If
      Next i
  Else
      NtoC = NtoC & "整"
  End If
Else
  NtoC = "零" & Yuan
End If
End Function

解决方案 »

  1.   

    微信软件工作室 (MicroInfo Soft Studio) 
    http://www.playyuer.com
    于溪玥(PlayYuer) 2000/02/01 帖:
    《VB6 之数据格式化对象使用技巧》(http://microinfo.top263.net/Txt/Format.txt
    )
    例程下载:(http://microinfo.top263.net/Zip/Format.exe)
    Visual Basic 提供了许多工具以满足数据访问编程的需要,其中引用微软数据格式化对象库
    (Microsoft Data Formatting Object Library) 格式化对象技术是 VB6 的增强功能。其最大优
    点是可以通过编程随心所欲地格式化数据(可参阅 MSDN )。下面以格式化 DataGrid 各列数据为
    例,演示数据格式化对象的使用方法。首先,根据 DataGrid 各列的数据类型,确定格式化后的格式:
          布尔(Boolean)型数据:True = 是;False = 否;
          数字(Number)型数据:格式化为大写;
    其次,确认工程已经引用(Reference)了Microsoft Data Formatting Object Library。
    再次,在窗体上绘制一个DataGrid控件。
    接下来,就可以编写代码了:1.声明带 WithEvents 关键字的 StdDataFormat 对象类型的窗体模块级的私有变量,作为大写
      格式的数据格式对象:  Option Explicit
      Private WithEvents FmtX As StdDataFormat
    2.编写Private Sub Form_Load()事件过程:  Private Sub Form_Load()
      '创建含有货币、布尔、数字型字段的记录集
      Dim adoRecordset As New ADODB.Recordset
      adoRecordset.Fields.Append "Fld1", adCurrency, , adFldIsNullable + adFldMayBeNull
      adoRecordset.Fields.Append "Fld2", adBoolean, , adFldIsNullable + adFldMayBeNull
      adoRecordset.Fields.Append "Fld3", adDouble, , adFldIsNullable + adFldMayBeNull
      adoRecordset.Open
      adoRecordset.AddNew Array("fld1", "fld2", "fld3"), Array(1001231.01, True, 634532.23)
      adoRecordset.AddNew Array("fld1", "fld2", "fld3"), Array(1000.23, False, 89758242)  Set DataGrid1.DataSource = adoRecordset  FormatDataGrid DataGrid1
      End Sub
    3.编写一个过程,格式化 DataGrid,可在需要的时机调用:  Sub FormatDataGrid(DataGridX As DataGrid)
          Dim FmtY As New StdDataFormat '用于布尔型字段格式
          FmtY.Type = fmtBoolean
          FmtY.TrueValue = "√" '"是" '"真" "对"
          FmtY.FalseValue = "×" '"否" '"假" "错"      Set FmtX = New StdDataFormat '实例化用于格式化货币型字段的对象
          FmtX.Type = fmtCustom '用于货币型字段格式      Dim adoRecordsetX As ADODB.Recordset
          Set adoRecordsetX = DataGridX.DataSource      Dim i As Integer
          For i = 0 To adoRecordsetX.Fields.Count - 1
              With DataGridX.Columns.Item(i)
                    Select Case adoRecordsetX.Fields.Item(i).Type  '可根据字段数据类型设置数据格式
                          Case adNumeric, adVarNumeric, _
                                adBigInt, adUnsignedInt, adUnsignedTinyInt, _
                                adUnsignedSmallInt, adUnsignedBigInt, _
                                adInteger, adSmallInt, adBigInt, adTinyInt, _
                                adDecimal, adDouble, adSingle '与数字有关的数据类型
                                .DataFormat.Format = "Standard"
                                .Alignment = dbgRight
                          Case adCurrency '货币型
                                Set .DataFormat = FmtX
                                .Alignment = dbgRight
                          Case adBoolean
                                Set .DataFormat = FmtY '布尔型字段设为自定义格式
                                .Alignment = dbgCenter
                          Case adDate, adDBDate, adDBTimeStamp
                                .DataFormat.Format = "Long Date" '日期、时间
                                .Alignment = dbgRight
                          Case Else '其它,如:文本等
                                .Alignment = dbgLeft
                        End Select
              End With
          Next i
      End Sub
    4.编写数据格式化对象的格式化 (Format) 事件过程:  Private Sub FmtX_Format(ByVal DataValue As StdFormat.StdDataValue)
      '读者可自行编写将数字转化为大写的函数!例如(附录: NtoC 函数):
      DataValue = NtoC(Trim(Str(DataValue)), "圆", "角", "分")
      End Sub  (该方案在 PWin 9.X 和 VB 6.0 下通过。)
        数据格式化对象,可以用于格式化各种具有 DataFormat 属性的控件,程序员可以将数据显示
    为各种格式。如果格式非常简单,可以使用传统的 Format 函数。如果格式布尔型数据,可以使用
    过程级的数据格式化对象。如果格式非常复杂,情况很多,则须使用窗体模块级的带事件数据格式
    化对象,并编程该对象的 Format 等事件。(欢迎去 Http://microinfo.top263.net 下载例程)================================================
    《 附录: NtoC 函数 》
    Option Explicit
    Public Function NtoC(ByVal sNum As String, Optional ByVal Yuan As String = "美圆", Optional ByVal Jiao As String = "美角", Optional ByVal Fen As String = "美分") As String
    If Val(Trim(sNum)) > 0 Then
      Dim sIntD, sDecD As String
      Dim i, iCount, j, iLength As Integer
      Dim lStartPos As Long
      Dim sBIT(4), sUNIT(3), sCents(2) As String
      sBIT(0) = "" '个
      sBIT(1) = "拾"
      sBIT(2) = "佰"
      sBIT(3) = "仟"
      sUNIT(0) = ""
      sUNIT(1) = "万"
      sUNIT(2) = "亿"
      sUNIT(3) = "yu"
      sCents(0) = Fen
      sCents(1) = Jiao
      Dim temp As String
      If InStr(Trim(sNum), ".") > 0 Then
          temp = Left(Trim(sNum), InStr(Trim(sNum), ".") - 1)
      Else
          temp = Trim(sNum)
      End If
      iCount = IIf(Len(temp) Mod 4, Len(Trim(temp)) \ 4 + 1, Len(Trim(temp)) \ 4)
      lStartPos = 1
      For i = iCount To 1 Step -1
          If i = iCount And Len(Trim(temp)) Mod 4 <> 0 Then
              iLength = Len(Trim(temp)) Mod 4
          Else
              iLength = 4
          End If
          sIntD = Mid(Trim(temp), lStartPos, iLength)
          For j = 1 To Len(Trim(sIntD))
              If Val(Mid(sIntD, j, 1)) <> 0 Then
                  NtoC = NtoC & Choose(Val(Mid(sIntD, j, 1)), "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖") & sBIT(Len(Trim(sIntD)) - j)
              Else
                  If Val(Mid(sIntD, j + 1, 1)) <> 0 Then
                    NtoC = NtoC & "零"
                  End If
              End If
          Next j
          lStartPos = lStartPos + iLength
          If i < iCount Then
              If (Val(Mid(sIntD, Len(Trim(sIntD)), 1)) <> 0 Or Val(Mid(sIntD, Len(Trim(sIntD)) - 1, 1)) <> 0 Or Val(Mid(sIntD, Len(Trim(sIntD)) - 2, 1)) Or Val(Mid(sIntD, Len(Trim(sIntD)) - 3, 1)) <> 0) Then
                NtoC = NtoC & sUNIT(i - 1)
              End If
          Else
              NtoC = NtoC & sUNIT(i - 1)
          End If
      Next
      If Len(Trim(NtoC)) > 0 Then
          NtoC = NtoC & Yuan
      End If
      '小数
      If InStr(1, Trim(sNum), ".") <> 0 Then
          sDecD = Right(sNum, Len(Trim(sNum)) - InStr(1, Trim(sNum), "."))
          For i = 1 To Len(Trim(sDecD))
              If Val(Mid(Trim(sDecD), i, 1)) <> 0 Then
                NtoC = NtoC & Choose(Val(Mid(Trim(sDecD), i, 1)), "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
                NtoC = NtoC & sCents(2 - i)
                If i >= 2 Then
                    Exit For
                End If
              Else
                If Len(Trim(NtoC)) > 0 Then
                    NtoC = NtoC & "零"
                End If
              End If
          Next i
      Else
          NtoC = NtoC & "整"
      End If
    Else
      NtoC = "零" & Yuan
    End If
    End Function
      

  2.   

    上面的兄台太夸张了吧,这样小问题不用这么多代码吧!!!
       直接用Datagrid.row控制就行了,代码当然是越精简越好!!!
      

  3.   

    我用如下代码实现:   Dim fmtBL As New StdDataFormat
                        fmtBL.Type = fmtBoolean
                        fmtBL.TrueValue = "√"
                        fmtBL.FalseValue = " "
                       Set DataGrid1.Columns("底层标志").DataFormat = fmtBL
                       Set DataGrid1.Columns("子集标志").DataFormat = fmtBL
                       Set DataGrid1.Columns("统计可见").DataFormat = fmtBL
                    
                        DataGrid1.Refresh 
                        DataGrid1.Visible = True
    但随意点击DataGrid1的单元格后,退出,则出现"DataGrid1多步操作产生错误,请检查每一步的操作值"的提示,按"确定"后正常退出.如果不点击DataGrid1的单元格,则无异常.这是为什么????