Questions

Adding items to the subitems in a listview in VB 2005

Tags:
+
0 Votes
Locked

Adding items to the subitems in a listview in VB 2005

rieda
Hi there

Below is some of my code in VB6 that use to add items to a listview
with subitems:

The listview is called lstvCustomers that gets data from a Access DB

I know how to access data from access and I know how to add the data
in the 1st column

my problem is adding the remaining data in the subitem of VB 2005

VB Version 6 Code:

Set Result = lstvCustomers.ListItems.Add(, , orec!Customer_AccNumber)
With Result
.SubItems(1) = orec!Customer_Name
.SubItems(2) = orec!Customer_Surname
.SubItems(3) = orec!ID_Provided
.SubItems(4) = orec!Customer_Phone
.SubItems(5) = orec!Customer_Fax
.SubItems(6) = orec!Customer_Cell
orec.MoveNext
End With

=====================================================================
VB 2005 Code:

oledbcon.Open()
Dim strSQL As String = "SELECT * FROM Customers"
Dim cmd As OleDbCommand = New OleDbCommand(strSQL,
oledbcon)
Dim objread As OleDbDataReader
objread = cmd.ExecuteReader

While objread.Read
lsvCustomers.Items.Add(objread("Customer_AccNumber"))
End While

objread.Close()
=====================================================================

im stuck by the part where i have to add subitems
my listview is designed the same way it was in VB6 with the 7 columns

Please Assist

Thanks

Mr Rieda Hoosain
  • +
    0 Votes
    alan williams

    the answer is in the library at

    Displaying Subitems in Columns with the Windows Forms ListView Control

    you can look online if you don't have your own copy.

    +
    0 Votes
    Lionfan1991

    There are a number of different ways, but one easy way is below.

    While objread.Read
    Dim LVI as ListViewItem = lsvCustomers.Items.Add(".")
    LVI.Text = objread("Customer_AccNumber")
    LVI.SubItems.Add(objread("Customer_Name")
    ...
    End While

    Note I take advantage of the fact the Add method to the ListViewItemCollection returns a ListViewItem which we can then use to set its properties (Text and SubItems).

    +
    0 Votes
    Les Tepper

    While objread.Read
    dim lvItem as New ListViewItem
    lsvCustomers.Items.Add(lvItem)
    lvItem.Text = objread("Customer_AccNumber")
    lvItem.SubItems.Add(objread("Customer_Name"))
    lvItem.SubItems.Add(objread("Customer_SurName"))
    .
    .
    .
    End While

  • +
    0 Votes
    alan williams

    the answer is in the library at

    Displaying Subitems in Columns with the Windows Forms ListView Control

    you can look online if you don't have your own copy.

    +
    0 Votes
    Lionfan1991

    There are a number of different ways, but one easy way is below.

    While objread.Read
    Dim LVI as ListViewItem = lsvCustomers.Items.Add(".")
    LVI.Text = objread("Customer_AccNumber")
    LVI.SubItems.Add(objread("Customer_Name")
    ...
    End While

    Note I take advantage of the fact the Add method to the ListViewItemCollection returns a ListViewItem which we can then use to set its properties (Text and SubItems).

    +
    0 Votes
    Les Tepper

    While objread.Read
    dim lvItem as New ListViewItem
    lsvCustomers.Items.Add(lvItem)
    lvItem.Text = objread("Customer_AccNumber")
    lvItem.SubItems.Add(objread("Customer_Name"))
    lvItem.SubItems.Add(objread("Customer_SurName"))
    .
    .
    .
    End While