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.
Printable View
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.
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:
For Each item As ListViewItem In myListView.Items For index As Integer = 0 To myListView.Columns.Count - 1 Step 1 MessageBox.Show(item.SubItems(index).Text) Next index Next item
Thanks, it worked.
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.
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.
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.
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.
can u please illustrate
add a "PrintDocument" control in your form and copy this code. I assume you have “PrintDocument1” and “Button1” controls on your form.
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim x As Integer = 150 Dim y As Integer = 80 Dim fontSize As Single = 12.0 For Each item As ListViewItem In myListView.Items For index As Integer = 0 To myListView.Columns.Count - 1 Step 1 e.Graphics.DrawString(item.SubItems(index).Text, New Font("Courier", fontSize, FontStyle.Regular), Brushes.Black, x, y) y = CInt(y + fontSize) Next index Next item End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.PrintDocument1.Print() End Sub