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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Columns to list view at run time??? 1

Status
Not open for further replies.

cyberbiker

Programmer
Mar 16, 2001
431
0
0
US
I need to find how to add columns to the list view control at run time.
Can it be done? If so, how? I cannot seem to figure it out.
I am reading a database with a user entered SQL Statement. I then need to display the results in a listview control. Since the recordset may contain from 1 to an unrealistically high number of fields, I need to create coulumns and adjust column widths accordingly.

The basic plan is something like this:

Private Sub sethead()
Dim f As Field, i As Integer
If rs.RecordCount > 0 Then
i = 1
For Each f In rs.Fields
CREATE COLUMN(i) HERE
lvRS.Column(i).header = f.name
column(i).Width = column(i).TextWidth
i = i + 1
Next
End If
End Sub

Then I will loop through the recordset adding the values.

I am stuck with using the listview control unless it proves to be impossible to create columns at run time.
Any help will be appreciated greatly. Terry (cyberbiker)
 
The code I am showing assumes the View property to be lvwReport with columnheaders. It is using an array but could be converted to a recordset very quickly.

Code:
    Dim itmX As ListItem
    Dim iLoop As Integer
    
    'Add columns to listview
    With lstTKVariable
        .ColumnHeaders.Add , , "Col a Name", lstView.Width * (1 / 6)
        .ColumnHeaders.Add , , "Col b Name", lstView.Width * (1 / 6)
        .ColumnHeaders.Add , , "Col c Name", lstView.Width * (4 / 6) 
        .GridLines = True
    End With

    For iLoop = 1 To UBound(garrReturn)
        Set itmX = lstTKVariable.ListItems.Add(, ,garrReturn(iLoop))
        itmX.SubItems(1) = garrReturn(iLoop)
        itmX.SubItems(2) = garrReturn(iLoop)
    Next iLoop

 
Replace:
CREATE COLUMN(i) HERE
lvRS.Column(i).header = f.name
column(i).Width = column(i).TextWidth

With:
ListView1.ColumnHeaders.Add i,,f.name,yourwidth

You'll have to play with Yourwidth, depending on number of fields and total listview width.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks for the help and the quick reply.
I must apologize for posting before rewording my search and trying a second time. There are several helpful posts on this subject already in the forums. Terry (cyberbiker)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top