温馨提示:本文翻译自stackoverflow.com,查看原文请点击:cycle - Vb6 Grouping Values in Flexgrid
cycle vb6

cycle - Flexgrid中的Vb6分组值

发布于 2020-04-30 16:34:18

我有一个从任何DB断开的Flexgrid(vb6都不是.Net),它具有一些这样的行:

135,00  4
218,00  4
100,00  10
6,00    4
15,00   22

我无法创建一个周期来获取网格上有多少个不同的组(按增值税),并计算相对数量,以便有3个组(在这种情况下为4/10/22)4 = 359,00 10 = 100 22 = 15,00我不想使用带有select group by的数据库,而是预先计算金额并在n文本框中显示。

这是我尝试过的代码

Dim A, b, c, d 
if A = "" Then
A = xNum
  ElseIf A <> "" Then
 If A <> xNum Then
 If b = "" Then
   b = xNum
     ElseIf b <> "" Then
        If b <> xNum Then
          If c = "" Then
             c = xNum
              ElseIf c <> "" Then
                 If c <> xNum Then
                    If d = "" Then
                       d = xNum
                        ElseIf d <> "" Then [etc...]

谢谢你的帮助。

查看更多

提问者
Jayelef
被浏览
8
Brian M Stafford 2020-02-12 04:31

我假设您使用的是Microsoft FlexGrid控件,其中第一列是数量,第二列是增值税。

在此处输入图片说明

以下代码将产生显示在文本框中的结果:

Option Explicit

Private Sub cmdCalc_Click()
   On Error Resume Next

   Dim c As Collection
   Dim i As Integer
   Dim g As Group
   Dim data As String

   'calculate groups
   Set c = New Collection

   For i = 1 To MSFlexGrid1.Rows - 1
      Set g = New Group
      g.VAT = CInt(MSFlexGrid1.TextMatrix(i, 2))
      g.Qty = CDbl(MSFlexGrid1.TextMatrix(i, 1))

      Err.Clear
      c.Add g, CStr(g.VAT)
      If Err.Number > 0 Then c(CStr(g.VAT)).Qty = c(CStr(g.VAT)).Qty + g.Qty
   Next

   'and visualize the data
   For Each g In c
      data = data & g.VAT & vbTab & g.Qty & vbCrLf
   Next

   Text1.Text = data
End Sub

此代码的基本思想是构建一组组对象,网格中的每一行一组。在将组添加到集合中时,错误陷阱可用于检测重复项并在这些情况下增加数量。

这是Group上面的代码使用类:

Option Explicit

Private m_VAT As Integer
Private m_Qty As Double

Public Property Get VAT() As Integer
   VAT = m_VAT
End Property

Public Property Let VAT(ByVal Value As Integer)
   m_VAT = Value
End Property

Public Property Get Qty() As Double
   Qty = m_Qty
End Property

Public Property Let Qty(ByVal Value As Double)
   m_Qty = Value
End Property