• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

mshflexgrid control, programmatically adding data

I would like to programatically add data to a mshflexgrid
control.  I cannot bind the control to my data, because the
data is in a format that will not support binding to the
control. I have two problems.  
First, when I loop through my data structure, assigning
the values to the grid, the first row in my grid
appears empty (the first row below what appears to be
the field name area ... so two rows, really).
Second, I would like to be able to assign field names. I
don't see properties to do that.  Is that only available if
my control is bound?
Here is some code from my procedure:

("theline" is a record from the source data structure)

        If theline Is Nothing Then
            ' do nothing
            ' populate a string
            For thecount = 0 To thefields.FieldCount - 1
                    thetabbedline = thetabbedline & vbTab & theline.Value(thecount)
                End If
            Next thecount
            If thetabbedline <> "" Then
            ' add string to grid
                MSFlexGrid1.AddItem thetabbedline
            End If
            thetabbedline = ""
            Set theline = thecursor.NextRow
        End If
    Loop Until theline Is Nothing

It occurs to me that there may be controls better
suited to my goal.  What I need to do is put records
into a grid, be able to identify what rows/columns/
cells are selected by the user, do some sorting,
do some aggregation (sum,mean,etc), export selected
or aggregated records (the least important piece).
If you think that there is another control, that ships
with VB6, Windows NT, or MSOffice2000, that will
better suit my needs, please let me know.

Thanks for your help.
  • 2
1 Solution
I think the easiest way is to have a disconnected recordset and populate that with your data. You can then set the grids datasource to the recordset and heh presto. You can the modify the data using the recordset
kroukAuthor Commented:
Hhmm. Well, I suppose that I could do that ... create a new
ADO recordset, adding my records one at a time, and then assigning that recordset to the control.  However, I
would have the cost of the additional ADO overhead plus,
I would have to write procedures to get data in and of
the recordset structure.  If there was simply a way to
put records into a grid control and specify field names,
that would solve my problem and save me some trouble.
Are you saying that you do not know of a way to do that?
My view is that the stucture and methods available to you as part of an ado recordset are easier to use than those of the flexgrid.
with adorec
  for lField = 0 to thefields.FieldCount - 1
    .fields.append "field" + cstr(lfield),advarchar
or just
  .fields.append "Value",adlongvarchar
end with

Then you can either store each field or concatenate them as you have and store in the one field.

The current field of the recordset will be that of the grid. so you can easily get the data out.

I prefer that than trying to get round the flexgrids methods  

I experienced the same issues described in the original question. Sure, my data could be bound to a data control - I just despise data controls!

I assume you refer to Column Headings as field names. These are set by using the ColHeaderCaption property as follows...

'Insert headers in grid
For i = 0 To 5
  gridTam.ColHeader(i) = flexColHeaderOn
Next i

With gridObj
  .ColHeaderCaption(0, 0) = "Index"
  .ColHeaderCaption(0, 1) = "Date"
  .ColHeaderCaption(0, 2) = "Time"
  .ColHeaderCaption(0, 3) = "Account"
  .ColHeaderCaption(0, 4) = "Category"
  .ColHeaderCaption(0, 5) = "Activity"
End With

- or -

Add the header line and make it a fixed row:

gridObj.Rows = 2
gridObj.FixedRows = 1
gridObj.AddItem "Index" & Chr(9) & "Date" & Chr(9) & "Time" & Chr(9) & "Account" & Chr(9) & "Category" & Chr(9) & "Activity"

I HATE that empty row! After hours of stepping thru the code and trying to understand WHY that empty row keeps showing up, the only way I found to fix it was to use:

RemoveItem(1)  'If .FixedRows = 1
RemoveItem(0)  'If .FixedRows = 0

Put this code AFTER populating the grid with records. This works because the empty row is always the first "non-fixed" row. This is simply a work-around since I have not been able to find any decent instructions on how to effectively use the MSHFlexGrid control. Powerful control, but no docs to be found...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now