Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how 2 assign an array to an msflexgrid? 2

Status
Not open for further replies.

N1GHTEYES

Technical User
Jun 18, 2004
771
GB
I know this is probably a very dumb question, but if so, I'm just being very dumb today. I have an array of user-defined (user-type) items. I want to populate an MSFgrid with this data. I could use a "For" loop, but that's a little messy and very slow (its about 30000 rows * 6 cols). There must be an easy way of associating the array directly to the grid, but I can't find it.

BTW, if you can possibly do so, please avoid the words "database" or DAO or similar in any replies. I will get round to learning about all that one of these days, but, for now, its all a foreign country to me.
 
No matter how you load 30000 rows it will seem slow. You need some sort of paging. That is to only load records that the user can see (one page). Then when they scroll you load another 'page' of records. The trick is to have a variable that keeps track of the record or page that is currently being displayed and a floating scroll bar that sits on top of the grids vertical scroll bar. It is not as hard as it sounds.

Here is a link to an example of recordset paging. You would just need to adjust it for array paging.

zemp
 
N1GHTEYES,

Put 2 column/2 row including 1 fixed column and 1 fixed row grid on a form. Size the grid but do not make it higher than 20 rows (this is page size in this example). Put one command button and name it cmdLoadFirstPage.

Start program, press that button once to load first page on the grid, and then scroll the grid. I wrote it in like 15 - 20 minutes so if something does not work well feel free post your questions.

Please note that I load your array in a loop anyway. This activity itself can take time if array is large.

Option Explicit

Private Const PAGE As Integer = 20
Private Const ARRAY_SIZE As Integer = 20000
Private mintArray(ARRAY_SIZE) As Integer
Private mlngPages As Long

Private Sub cmdLoadFirstPage_Click()
AddPage
MSHFlexGrid1.RemoveItem 1
End Sub

Private Sub Form_Load()
PopulateArray
mlngPages = 0
End Sub

Private Sub MSHFlexGrid1_Scroll()
If MSHFlexGrid1.RowIsVisible(mlngPages) = False Then
AddPage
End If
End Sub

Private Sub AddPage()
Dim i As Long

If (mlngPages) * PAGE < UBound(mintArray) Then
mlngPages = mlngPages + 1

For i = (mlngPages - 1) * PAGE To mlngPages * PAGE - 1
MSHFlexGrid1.AddItem vbTab & mintArray(i) + 1
Next i
End If

End Sub

Private Sub PopulateArray()
Dim i As Long

For i = 0 To ARRAY_SIZE
mintArray(i) = i
Next i
End Sub

vladk
 
N1GHTEYES,

This version populates your array also page by page only when needed:

Option Explicit

Private Const PAGE As Integer = 20
Private mintPagedArray() As Integer
Private mlngPages As Long

Private Sub cmdLoadFirstPage_Click()
PopulatePagedArray
AddPage
MSHFlexGrid1.RemoveItem 1
cmdLoadFirstPage.Enabled = False
End Sub

Private Sub MSHFlexGrid1_Scroll()
If MSHFlexGrid1.RowIsVisible(mlngPages) = False Then
PopulatePagedArray
AddPage
End If
End Sub

Private Sub AddPage()
Dim i As Long

If (mlngPages - 1) * (PAGE) <= MSHFlexGrid1.TopRow Then
If mlngPages * PAGE < UBound(mintPagedArray) Then
mlngPages = mlngPages + 1

For i = (mlngPages - 1) * PAGE To mlngPages * PAGE - 1
MSHFlexGrid1.AddItem vbTab & mintPagedArray(i)
Next i
End If
End If

End Sub

Private Sub PopulatePagedArray()
Dim i As Long

ReDim Preserve mintPagedArray((mlngPages + 1) * PAGE)

For i = mlngPages * PAGE + 1 To (mlngPages + 1) * PAGE
mintPagedArray(i - 1) = i
Next i

End Sub

vladk
 
Thanks for the paging code everybody. I think I will go that way. However, I'm still curious - can you or can't you assign an array directly to a grid? If so - how?

If you can't do it with an MSFlexgrid, can it be done with any other available grid (HSHFlexgrid, DBGrid etc)?

It seems to me that you can do this with the datagrid associated with the MSChart, so I'm guessing you can somehow do the same with grids in general - I just can't find a refererence to how you do it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top