1

我编写了以下代码来绑定数据组合

With dtcType
        Set .RowSource = recType
            .ListField = "Type"
            .BoundColumn = "TypeId"
End With

我想在 DataCombo 顶部添加一个空白项(绑定 datacombo 后,用户也可以选择一个空白项)。我怎样才能做到这一点?

4

1 回答 1

1

据我记得没有直接的方法来添加 item do DataCombo。您可以考虑两种选择。

如果您的 SQL 看起来更像这样:

SELECT Id, Code FROM my_table

SELECT您可以使用无FROM子句结合添加人工记录UNION

SELECT -1 As Id, '' As Code 
UNION ALL
SELECT Id, Code FROM my_table

第二个选项是用这样的方法替换DataComboCombobox填充项目:

Public Sub Populate(ByRef pData As ADODB.Recordset, _
                ByVal pIdField As String, _
                ByVal pDataField As String, _
                Optional pEmptyItem As Boolean = False, _
                Optional pEmptyItemId As Long = -2, _
                Optional pEmptyItemCaption As String = "")

    If m_ComboboxControl Is Nothing Then Exit Sub

    If pEmptyItem Then
        m_ComboboxControl.AddItem pEmptyItemCaption
        m_ComboboxControl.ItemData(m_ComboboxControl.NewIndex) = pEmptyItemId
    End If   

    With pData

        If Not (.BOF And .EOF) Then
            Do While Not .EOF
                Select Case TypeName(pData.Fields(pDataField).Value)
                    Case "Date"
                        m_ComboboxControl.AddItem Format(pData.Fields(pDataField), "DD-MM-YYYY")
                    Case Else
                        m_ComboboxControl.AddItem pData.Fields(pDataField)
                End Select
                m_ComboboxControl.ItemData(m_ComboboxControl.NewIndex) = CLng(pData.Fields(pIdField))

                .MoveNext
            Loop
        End If


    End With

End Sub
于 2012-06-12T07:22:24.223 回答