Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am having the hardest time with ListView today. The code looks good but when i run it i do not see data in the ListView (it's just totally blank). Please help, I have tried everything but still can't get it to working


VB
Dim T As New Form1

Dim arr(3) As String

arr(0) = "a"
arr(1) = "b"
arr(2) = "c"
arr(3) = "d"

Dim itm As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(arr)

T.ListViewRev.Items.Add(itm)

T.ShowDialog()
Posted
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 23:17pm    
Please add the tag: WinForms.
—SA

Hi,

i suggest you to bind the list view on Load event of Form1.

VB
'Open the Form1
Dim T As New Form1
T.ShowDialog()

VB
'Form1 Load event
Protected Sub Form1_Load(sender As Object, e As EventArgs)

'Bind the list view
Dim arr(3) As String
 
arr(0) = "a"
arr(1) = "b"
arr(2) = "c"
arr(3) = "d"
 
Dim itm As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(arr)
 
ListViewRev.Items.Add(itm)
End Sub


hope it works.
 
Share this answer
 
Comments
juno101 19-Feb-13 11:42am    
I guess the only way to populate the ListView is to have the code run inside the Form Sub (Form1_Load). Interesting, considering that one can populate a ListBox via creating a Form object and dumping data into the TextBox, ListBox . . . .

I still don't understand . . . I am able to just move my code to the Form Sub. However it would be nice to understand why one can not populate data via creating an instance of the Form class and pushing data into it's ListView .
This is not how it works. This constructor of ListViewItem creates an item with sub-items:
http://msdn.microsoft.com/en-us/library/faw83h4f.aspx[^].

Sub-items are used in the View with the value System.Windows.Forms.View.Details, which show Columns, so you need to set the View property, define columns, etc. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.view.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.view.aspx[^].

The last MSDN page referenced above shows a code sample with Details, Columns and sub-items. If this is what you wanted, check it and add what is missing from your code. If you need different kind of view, read the documentation more.

—SA
 
Share this answer
 
I think the problem was that i needed to add a column
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900