2013-01-07 84 views
0

所以我必須從網格中保存有兩種類型的ID。 LineId & CustId。每個LineId可以有多個CustIds。數據的一個例子是這樣的:需要保存一對多關係VB6

LineId  CustId 
1   33 
2   98 
7   101 
1   51 
3   28 
7   02 
1   35   

我需要保存代碼與接受CustIds每個行標識的空分隔字符串保存過程。我爲保存的每個LineId調用一次保存過程。我無法改變保存程序的工作方式。

到目前爲止,我一直在將網格添加到帶有行ID和cust id的類型數組中。

Dim typeRows(gridRows - 1) As Ids 'gridRows is rowcount of grid 
For i = 0 To grid.Rows - 1 
    typeRows(i).LineId = grid.TextMatrix(i, GridColumns.colLineId) 
    typeRows(i).CustId = grid.TextMatrix(i, GridColumns.colCustId) 
Next 

但我有點卡在接下來應該做的事情上。我應該對網格進行排序嗎?那麼我將如何通過排序的網格併爲每個lineid組合cust id?

任何幫助或指導將不勝感激。

+0

我沒有看到您的問題,只要行中的數據保持在同一行上,您只需從行中獲取兩個值即可。 – jmoreno

+0

創建一個由空分隔字符串組成的數組(大小爲網格),對列表進行排序,循環遍歷列表,只要lineid保持不變,將line custid添加到空分隔字符串,當lineid更改時創建一個新的空分隔字符串並增加一個計數器來跟蹤有多少零分隔字符串正在使用中。循環完成後保存所有正在使用的空分隔字符串(循環直到計數器值) – Hrqls

回答

2

不排序您通過數組必須循環以及檢查,如果ID已經發現之前的網格,可以做的很好,但是這取決於你的數據的大小,可能需要一些時間

'1 form with : 
' 1 flexgrid control : name=grid 
Option Explicit 

Private Type Ids 
    LineId As String 
    CustId As String 
End Type 

Private Sub Form_Click() 
    'export to delimited string 
    Dim intRow As Integer 
    Dim intId As Integer 
    Dim intBound As Integer 
    Dim udtIds() As Ids 
    Dim blnThere As Boolean 
    Dim intCount As Integer 
    With grid 
    intBound = .Rows - 1 
    ReDim udtIds(intBound) As Ids 
    intCount = 0 
    For intRow = 1 To intBound 
     'loop through all rows 
     blnThere = False 
     For intId = 0 To intBound 
     'check if lineid was already found before 
     If udtIds(intId).LineId = .TextMatrix(intRow, 0) Then 
      blnThere = True 
      Exit For 
     End If 
     Next intId 
     If blnThere Then 
     'if lineid was already found before, just add the custid 
     udtIds(intId).CustId = udtIds(intId).CustId & "," & .TextMatrix(intRow, 1) 
     Else 
     'if lineid is new, then create new lineid with custid in udt 
     udtIds(intCount).LineId = .TextMatrix(intRow, 0) 
     udtIds(intCount).CustId = .TextMatrix(intRow, 1) 
     intCount = intCount + 1 
     End If 
    Next intRow 
    End With 'grid 
    'now you can save the udt 
End Sub 

Private Sub Form_Load() 
    With grid 
    'fill grid with example values 
    .Cols = 2 
    .Rows = 8 
    .FixedRows = 1 
    .FixedCols = 0 
    .TextMatrix(0, 0) = "LineId" 
    .TextMatrix(0, 1) = "CustId" 
    .TextMatrix(1, 0) = "1" 
    .TextMatrix(2, 0) = "2" 
    .TextMatrix(3, 0) = "7" 
    .TextMatrix(4, 0) = "1" 
    .TextMatrix(5, 0) = "3" 
    .TextMatrix(6, 0) = "7" 
    .TextMatrix(7, 0) = "1" 
    .TextMatrix(1, 1) = "33" 
    .TextMatrix(2, 1) = "98" 
    .TextMatrix(3, 1) = "101" 
    .TextMatrix(4, 1) = "51" 
    .TextMatrix(5, 1) = "28" 
    .TextMatrix(6, 1) = "02" 
    .TextMatrix(7, 1) = "35" 
    End With 'grid 
End Sub