Public Sub FillGrid(MyGrid As MSFlexGrid, MyRs As ADODB.Recordset) 
  Dim i As Integer
  With MyGrid
     .Rows = 1
     .Row = 0
     .Cols = MyRs.Fields.Count + 1
     For i = 0 To MyRs.Fields.Count - 1 
      .Col = i + 1
       .Text = MyRs.Fields(i).Name
     Next
     i = 1
     Do While MyRs.EOF = False
        .AddItem  MyRs(0) & vbTab & MyRs(1) & …… <---就是在这里,因为字段的不同而不同,我到底写多少MyRs(0)和vbTab 未知 ,请指教,谢谢!!
        MyRs.MoveNext: i = i + 1
     Loop
  End With
End Sub

解决方案 »

  1.   

    这样写试试,(随手写得,没有测试:)Public Sub FillGrid(MyGrid As MSFlexGrid, MyRs As ADODB.Recordset) 
      Dim i As Integer
      Dim ItemStr as String
      With MyGrid
         .Rows = 1
         .Row = 0
         .Cols = MyRs.Fields.Count + 1
        If MyRs.Fields.Count=1 then
           .Col=1
           .Text=MyRs.Fields(0).Name
           ItemStr=MyRs.Fields(0)
        Else
           For i = 0 To MyRs.Fields.Count - 1 
             .Col = i + 1
             .Text = MyRs.Fields(i).Name
             ItemStr=ItemStr & vbTab & MyRs.Fields(i)
           Next
        End if
         i = 1
         Do While MyRs.EOF = False
            .AddItem  ItemStr
            MyRs.MoveNext
          i = i + 1
         Loop
      End With
    End Sub
      

  2.   

    '我笨啊!以前用过类似的方法,呵呵!!
    '感谢你的思想,以下程序测试通过Public Function FillGrid(MyGrid As MSFlexGrid, MyRs As ADODB.Recordset) As Boolean
      Dim i As Integer
      With MyGrid
         .Rows = 1
         .Row = 0
         .Cols = MyRs.Fields.Count + 1
         For i = 0 To MyRs.Fields.Count - 1 '初始化字段
           .Col = i + 1
           .Text = MyRs.Fields(i).Name
         Next
         j = 1
         
         Do While MyRs.EOF = False
              ItemStr = ""
              For i = 0 To MyRs.Fields.Count - 1
                .Col = i + 1
                ItemStr = ItemStr & vbTab & MyRs.Fields(i)
              Next
             .AddItem j & ItemStr
             
              If MyRs.Fields(2) = "×" Then .Row = .Rows - 1: .Col = 3: .CellForeColor = vbRed          MyRs.MoveNext: j = j + 1
         Loop  End With
    End Function