views:

1354

answers:

3

Hi guys.

When I add new values to a listview using :

    Set lstView = ListView(0).ListItems.Add(, , txtName)
    lstView.ListSubItems.Add , , txtValue
    lstView.Refresh

The only problem is that this only displays a blank new line in the listview, any idea how to update it correctly?

Normally I am using a recordset so simply clear then repopulate the data but I need the user to be able to add entries to the listview. I will then cycle through the listview adding the values tot he DB only once the user has finished amending the listview.

Thanks in advance for any help.

A: 

I personally like to use the string[] override for each column I have in my list view.

ListViewItem listItem = new ListViewItem( new string[] { "column 1 value", 
                                                         "column 2 value" } );
listItem.Tag = myObjectProvidingTheValues;
listView_myListView.Items.Add( listItem );

EDIT: Well, I gave you the C# code and you wanted the VB code. I'm not sure what that is. If you are using .NET, the same concepts should apply. If you are using VB6, I'm not sure what the exact nuance is.

j0rd4n
A: 

Assuming the .View property of your ListView is set to "Report", the following will add a couple of rows to the control and set the sub item text.

Dim li As ListItem

With ListView1
    .ColumnHeaders.Add , , "One"
    .ColumnHeaders.Add , , "Two"
    .ColumnHeaders.Add , , "Three"

    Set li = .ListItems.Add(, , "Row1Item1")
    li.SubItems(1) = "Row1Item2"
    li.SubItems(2) = "Row1Item3"

    Set li = .ListItems.Add(, , "Row2Item1")
    li.SubItems(1) = "Row2Item2"
    li.SubItems(2) = "Row2Item3"
End With
Jim H.
This is not unlike the code I already have in the question if I'm reading it correctly? My problem is that if I then wanted to go and add another row of data at a later point the row of data does not show in the listview, it is not updating itself.
Denvar
A: 

Ignore me this was due to an unrelated (seemingly) issue which has now been resolved. The previous posters listed the correct ways of adding items to listviews, as did the original code in my question.

Denvar