【问题标题】:Copying the values from a DataGrid to an Array vb6将值从 DataGrid 复制到数组 vb6
【发布时间】:2012-06-19 13:24:43
【问题描述】:

我有一个问题想要解决。

我想在 adodc 更新数据库中的记录之前检查一列是否在数据网格的行中重复了一个值。

为此,我将获取 DataGrid 中显示的记录并将它们放入一个数组中,然后计算单元格中具有相同值的行数。

我遇到的问题是我不能将 DataGrid 的单元格放在一个数组中,所以我可以检查重复的行。

如何在数组中获取 DataGrid 的单元格? 或者在数据库中进行更新之前,我如何知道某行的值是否重复?

【问题讨论】:

  • 通常您通过在数据库中使用约束来捕获尝试的重复项。 DataGrid 和其他绑定控件旨在通过 DataGrid 的 Error 事件等机制向您报告违反约束的情况。 DataGrid 不适合在未绑定模式下与大量程序逻辑一起使用。

标签: datagrid vb6


【解决方案1】:

我同意@bob-riemersma 的观点,即您的验证应该通过数据绑定和数据库约束来完成,但如果您想手动完成(我认为这是一个坏主意),我会使用集合:

Private Function GridColHasDuplicates(col As Long) As Boolean
    On Error GoTo DupeError
    Dim noDupes As New Collection

    myGrid.row = 0
    myGrid.col = col
    Do While myGrid.row <= myGrid.VisibleRows - 1
        noDupes.Add myGrid.Columns(col).Value ' this will error off if dupe
        If myGrid.row < myGrid.VisibleRows - 1 Then
            myGrid.row = myGrid.row + 1
        Else
            Exit Do
        End If
    Loop

    GridColHasDuplicates = False
    Exit Function
DupeError:
    GridColHasDuplicates = True

End Function

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多