Results 1 to 9 of 9

Thread: [02/03] Retrieving ListView Column values

  1. #1

    Thread Starter
    Addicted Member rpk_20061975's Avatar
    Join Date
    Jun 2001
    Location
    India
    Posts
    234

    [02/03] Retrieving ListView Column values

    I have a ListView Control with five columns and four rows. I want to retrieve each column value row-wise. I want to know how to retreive column values by executing a loop.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving ListView Column values

    Each row is a ListViewItem. Each item has ListViewSubItems. The Text properties of the subitems contain the strings you see displayed in the control:
    VB Code:
    1. For Each item As ListViewItem In myListView.Items
    2.     For index As Integer = 0 To myListView.Columns.Count - 1 Step 1
    3.         MessageBox.Show(item.SubItems(index).Text)
    4.     Next index
    5. Next item
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member rpk_20061975's Avatar
    Join Date
    Jun 2001
    Location
    India
    Posts
    234

    Re: [02/03] Retrieving ListView Column values

    Thanks, it worked.

  4. #4

    Thread Starter
    Addicted Member rpk_20061975's Avatar
    Join Date
    Jun 2001
    Location
    India
    Posts
    234

    Re: [02/03] Retrieving ListView Column values

    jm,

    One more thing I want to know is that I want to store column values in an array or something like that so that after the loop finishes, I can retrieve them.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving ListView Column values

    I don't get it. You're the one who put the values into the ListView in the first place so why do you need to then retrieve the values from the ListView into an array? If you need the values then you should be keeping them in an array or whatever when you put them in the ListView in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member rpk_20061975's Avatar
    Join Date
    Jun 2001
    Location
    India
    Posts
    234

    Re: [02/03] Retrieving ListView Column values

    jm,

    The ListView shows the user the items he has selected. When the user clicks the PRINT button, each row of the ListView gets printed on a different sheet. Actually it is a ticketing software.

    I am not storing values in an array because, I want to display the user choices so that he can modify or delete the selection.

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [02/03] Retrieving ListView Column values

    you can print them right from the listvew control in the loop as jm shows. You don't need to put them in an arry and then print them.

  8. #8

    Thread Starter
    Addicted Member rpk_20061975's Avatar
    Join Date
    Jun 2001
    Location
    India
    Posts
    234

    Re: [02/03] Retrieving ListView Column values

    can u please illustrate

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [02/03] Retrieving ListView Column values

    add a "PrintDocument" control in your form and copy this code. I assume you have “PrintDocument1” and “Button1” controls on your form.
    VB Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim x As Integer = 150
    3.         Dim y As Integer = 80
    4.         Dim fontSize As Single = 12.0
    5.         For Each item As ListViewItem In myListView.Items
    6.             For index As Integer = 0 To myListView.Columns.Count - 1 Step 1
    7.                 e.Graphics.DrawString(item.SubItems(index).Text, New Font("Courier", fontSize, FontStyle.Regular), Brushes.Black, x, y)
    8.                 y = CInt(y + fontSize)
    9.             Next index
    10.         Next item
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Me.PrintDocument1.Print()
    15.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width