Results 1 to 10 of 10

Thread: how to show a lots of data though MSFlexGrid [*resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    17

    how to show a lots of data though MSFlexGrid [*resolved]

    I need to read a text file and output the data
    I tried the code below
    but because the data is so many that it takes much time to run
    how to faster it?
    please help me, thanks very much


    '=======================================
    TotalNum = 0 'the total row of the file, about 7050

    'first loop to get the row of the file
    Open App.Path & "\1.dat" For Input As #1
    Do While Not EOF(1)
    Line Input #1, MyData1
    TotalNum = TotalNum + 1
    Loop
    Close #1

    MSFlexGrid1.Rows = TotalNum + 1
    'the second loop to send data to MSFlexGrid1
    i = 0
    Open App.Path & "\1.dat" For Input As #1
    Do While Not EOF(1)
    Line Input #1, MyData1
    i = i + 1
    TempString = MyData1
    MyData1 = Split(TempString, ",")
    With MSFlexGrid1
    .Row = i
    .Col = 0
    .Text = MyData1(0)
    .Col = 1
    .Text = MyData1(1)
    .Col = 2
    .Text = MyData1(2)
    .Col = 3
    .Text = MyData1(3)
    .Col = 4
    .Text = MyData1(4)
    End With
    Loop
    '========================================


    a part of the text file

    2.,217.6604,199.8500,199.8500,200.4962
    4.,227.5204,199.8500,199.8500,201.0546
    6.,234.5468,199.8501,199.8500,201.5784
    8.,240.2048,199.8503,199.8500,202.0836
    10.,245.0417,199.8509,199.8500,202.5763
    12.,249.3170,199.8520,199.8500,203.0596
    14.,253.1766,199.8542,199.8500,203.5354
    16.,256.7126,199.8580,199.8500,204.0048
    18.,259.9873,199.8641,199.8500,204.4688
    20.,263.0456,199.8732,199.8500,204.9279
    22.,265.9207,199.8861,199.8500,205.3826
    24.,268.6383,199.9038,199.8500,205.8334
    26.,271.2185,199.9271,199.8500,206.2806
    28.,273.6777,199.9569,199.8500,206.7244
    30.,276.0292,199.9939,199.8500,207.1650
    32.,278.2840,200.0390,199.8500,207.6028
    34.,280.4515,200.0927,199.8500,208.0378
    36.,282.5396,200.1556,199.8500,208.4702
    38.,284.5551,200.2283,199.8500,208.9001
    40.,286.5039,200.3111,199.8500,209.3276
    42.,288.3912,200.4043,199.8500,209.7530
    44.,290.2215,200.5082,199.8500,210.1761
    46.,291.9988,200.6230,199.8500,210.5972
    48.,293.7268,200.7487,199.8500,211.0163
    50.,295.4086,200.8853,199.8500,211.4335
    52.,297.0470,201.0329,199.8500,211.8489
    54.,298.6447,201.1914,199.8500,212.2624
    56.,300.2041,201.3606,199.8501,212.6743
    58.,301.7272,201.5403,199.8501,213.0844
    60.,303.2160,201.7306,199.8501,213.4930
    Last edited by junny; Dec 2nd, 2004 at 08:22 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    First of all, you shouldn't be using .Text if you want speed, as setting the row/col fires some events (so is slow). Use .TextMatrix instead, eg:

    VB Code:
    1. 'instead of this:
    2.       .Row = i
    3.       .Col = 0
    4.       .Text = MyData1(0)
    5.       .Col = 1
    6.       .Text = MyData1(1)
    7.       ...
    8. 'use this:
    9.       .TextMatrix(i, 0) = MyData1(0)
    10.       .TextMatrix(i, 1) = MyData1(1)
    11.       ...

    Next, you can use better methods of putting large amounts of data into the grid.

    The one that seems best in this case is AddItem, here's how it could work:
    VB Code:
    1. MSFlexGrid1.Rows = 1
    2.   'load file and put the data in to MSFlexGrid1
    3.   Open App.Path & "\1.dat" For Input As #1
    4.   Do While Not EOF(1)
    5.     Line Input #1, MyData1
    6.     MSFlexGrid1.AddItem Replace(MyData1, ",", vbTab)
    7.   Loop
    8.   MSFlexGrid1.RemoveItem 1   '(remove blank row)
    This has the bonus of only reading the file once, which will make the whole process much faster if the file is large.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    17
    thanks very much
    it does work!
    my code cost 46 seconds
    and yours only 26S

    but as you know, it still seems slow, is there some method else?

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    junny,

    Also why do you read thru the file first then fill the grid? There is no need for this. Do both at the same time.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    17
    I tried the code below,but it doesn't seem to faster
    thanks for your reply all the same


    VB Code:
    1. Option Explicit
    2. Dim time
    3. Dim time1
    4. Private Sub Form_Load()
    5. Call now_time
    6. Dim i, j As Integer
    7. Dim mydata1(10000)
    8.  
    9.  
    10.   'load file and put the data in to MSFlexGrid1
    11.   i = 0
    12.   Open App.Path & "\1.dat" For Input As #1
    13.   Do While Not EOF(1)
    14.     Line Input #1, mydata1(i)
    15.     i = i + 1
    16.   Loop
    17.  
    18. MSFlexGrid1.Rows = 1
    19. For j = 0 To i
    20.    MSFlexGrid1.AddItem Replace(mydata1(j), ",", vbTab)
    21. Next
    22.  
    23. Call now_time1
    24.  
    25. MsgBox Second(time1 - time)
    26. End Sub
    27.  
    28.  
    29. Private Sub now_time()
    30. time = Now()
    31. End Sub
    32.  
    33. Private Sub now_time1()
    34. time1 = Now()
    35. End Sub

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    junny


    I meant your code should look something like this:

    VB Code:
    1. Option Explicit
    2. Dim time
    3. Dim time1
    4.  
    5. Private Sub Form_Load()
    6.  
    7. Dim i As Integer
    8. Dim mydata1
    9.  
    10. Call now_time
    11.  
    12.   'load file and put the data in to MSFlexGrid1
    13.  
    14.  MSFlexGrid1.Rows = 1
    15.  i = 0
    16.   Open App.Path & "\1.dat" For Input As #1
    17.  
    18.   Do While Not EOF(1)
    19.       Line Input #1, mydata
    20.       MSFlexGrid1.AddItem Replace(mydata, ",", vbTab)
    21.       i = i + 1
    22.   Loop
    23.  
    24. Call now_time1
    25.  
    26. MsgBox Second(time1 - time)
    27. End Sub
    28.  
    29.  
    30. Private Sub now_time()
    31. time = Now()
    32. End Sub
    33.  
    34. Private Sub now_time1()
    35. time1 = Now()
    36. End Sub

  7. #7
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    Also try setting the Redraw property to false while your loading the info then set it basck to true afterwards.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    17
    thanks you all very much!!!!!!!!!!!!
    I used the code below and it's so fast that cost only 1 second!
    the process of redaw spends so much time!
    thank DeadEyes,and thank you all !


    VB Code:
    1. Option Explicit
    2. Dim time
    3. Dim time1
    4. Private Sub Form_Load()
    5. Call now_time
    6.  
    7. Dim mydata1
    8.  MSFlexGrid1.Rows = 1
    9.  MSFlexGrid1.Redraw = False
    10.   'load file and put the data in to MSFlexGrid1
    11.   Open App.Path & "\1.dat" For Input As #1
    12.   Do While Not EOF(1)
    13.     Line Input #1, mydata1
    14.     MSFlexGrid1.AddItem Replace(mydata1, ",", vbTab)
    15.   Loop
    16. Call now_time1
    17. MSFlexGrid1.Redraw = True
    18. MsgBox Second(time1 - time)
    19. End Sub
    20.  
    21.  
    22. Private Sub now_time()
    23. time = Now()
    24. End Sub
    25.  
    26. Private Sub now_time1()
    27. time1 = Now()
    28. End Sub

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    junny

    You should always make a habit of fully defining you dim statements. The way you define them as variants instrad of what they actually are such as date, string, long, integer etc...

    You will avoid problems in the future and variants cost more in space and processing time.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    17

    Re: how to show a lots of data though MSFlexGrid [*resolved]

    thanks for your suggestion
    and I will correct my habits


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width