|
-
Mar 19th, 2014, 12:49 PM
#1
Thread Starter
Fanatic Member
Adding items to a listview
Sorry about this silly question but I haven't done any Winforms work or VB in ages.
I am looping through a collection and adding items to a listvew control with the following code:
Code:
For Each f As FDayResult In failedResults
Dim item1 As New ListViewItem("lstItem")
item1.SubItems.Add(f.QTY.ToString())
item1.SubItems.Add(f.ProductCode)
ListView1.Items.Add(item1)
Next
All my listview shows is : item1 item1item1item1 in the horizontal.
Hmmmmmmm
I'm embaressed.
-
Mar 19th, 2014, 01:19 PM
#2
Re: Adding items to a listview
does it show "item1" or "lstItem" ??? if it shows "lstItem"... then sure... because that's what you set as the text when you created it. Presumably you want some other value, like one of the properties of f.
Now, if what you meant is why you aren't seeing the subitems, then it's because you're in one of the icon modes - which is the default. Try switching it to Report or Detail (it used to be one, now it's called the other... I don't remember which is the old name or the new name, but only one of those two should be available as the mode in the properties window... choose wisely).
-tg
-
Mar 19th, 2014, 01:25 PM
#3
Addicted Member
Re: Adding items to a listview
I think you'll want to actually instantiate your sub items so you can assign the Text property. There may be a better solution but this works...
Code:
Dim item1 As New ListViewItem("lstItem")
For i As Integer = 1 To 5
Dim si As New ListViewItem.ListViewSubItem()
si.Text = "Qty" & i
item1.SubItems.Add(si)
Next
ListView1.Items.Add(item1)
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Mar 19th, 2014, 01:30 PM
#4
Addicted Member
Re: Adding items to a listview
 Originally Posted by techgnome
does it show "item1" or "lstItem" ??? if it shows "lstItem"... then sure... because that's what you set as the text when you created it. Presumably you want some other value, like one of the properties of f.
Now, if what you meant is why you aren't seeing the subitems, then it's because you're in one of the icon modes - which is the default. Try switching it to Report or Detail (it used to be one, now it's called the other... I don't remember which is the old name or the new name, but only one of those two should be available as the mode in the properties window... choose wisely).
-tg
technome is correct, Details view and you'll need to add columns to the listview when you switch to this view type. 1 column for the main item and 1 column for each subitem.
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Mar 20th, 2014, 04:18 AM
#5
Thread Starter
Fanatic Member
Re: Adding items to a listview
Thanks for that guys but to be honest I am not sure what you mean. I have set the view to Details mode but now My column heqders just show "Column 1, Column 2" e.t.cand the first column shows "lstitem" and not f.qty.tostring() which appears under "Column 1".
I have used the designer to add columns but what do I need to do next?
Last edited by venerable bede; Mar 20th, 2014 at 04:22 AM.
-
Mar 20th, 2014, 04:21 AM
#6
Re: Adding items to a listview
the lv has a View property that you need to set to Details
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 20th, 2014, 04:42 AM
#7
Thread Starter
Fanatic Member
Re: Adding items to a listview
Ooops.
I think I edited my post while you where answering.
I think my problem is that I dont want the parent . All I want is a plain old list of items with column headers but can figure out how to achieve this.
-
Mar 20th, 2014, 04:49 AM
#8
Re: Adding items to a listview
you can edit the columnHeader Text in the designer, just right click the lv + each columnHeader has a Text property
Code:
For Each f As FDayResult In failedResults
Dim item1 As New ListViewItem(f.QTY.ToString())
item1.SubItems.Add(f.ProductCode)
ListView1.Items.Add(item1)
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 20th, 2014, 08:00 AM
#9
Addicted Member
Re: Adding items to a listview
I think this will get you to the path you want to get to. I was surprised to find out that when I added the "1stItem" as my ListViewItem it also took on the position of the first subitem at index 0.
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim item1 As New ListViewItem("lstItem")
Dim si As ListViewItem.ListViewSubItem
Me.ListView1.View = View.Details
Me.ListView1.Columns.Add("AnItem")
Me.ListView1.Columns(0).Width = 0 'Hide the column of the 1stItem
Me.ListView1.Columns.Add("Quantity", 50)
Me.ListView1.Columns.Add("Product Code", 200)
si = New ListViewItem.ListViewSubItem
si.Text = "Qty"
item1.SubItems.Add(si)
si = New ListViewItem.ListViewSubItem
si.Text = "ProductCode"
item1.SubItems.Add(si)
For i As Integer = 1 To 5
Dim item As New ListViewItem
item = item1.Clone
item.SubItems(1).Text = item.SubItems(1).Text & i.ToString
item.SubItems(2).Text = item.SubItems(2).Text & i.ToString
ListView1.Items.Add(item)
Next
End Sub
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Mar 20th, 2014, 08:13 AM
#10
Addicted Member
Re: Adding items to a listview
... thinking more about it I wanted to give you an example more to your actual effort...
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Test Data
Dim thedata As New DataTable
thedata = MakeFakeTestDataTable()
'Setup the ListView
Me.ListView1.View = View.Details
Me.ListView1.Columns.Add("AnItem")
Me.ListView1.Columns(0).Width = 0 'Hide the column of the 1stItem
Me.ListView1.Columns.Add("Qtty", 50).Name = "Quantity"
Me.ListView1.Columns.Add("Prd Cde", 200).Name = "Product Code"
'Add Items
For Each dr As DataRow In thedata.Rows
Dim item1 As New ListViewItem("lstItem")
Dim si As New ListViewItem.ListViewSubItem
'Quantity
item1.Text = dr(0)
si.Text = dr(1).ToString
item1.SubItems.Add(si)
'Product Code
si = New ListViewItem.ListViewSubItem
si.Text = dr(2)
item1.SubItems.Add(si)
ListView1.Items.Add(item1)
Next
End Sub
Private Function MakeFakeTestDataTable() As DataTable
'Setup fake test data to model venerable bede's data
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add("Items")
dt.Columns.Add("Qty")
dt.Columns.Add("ProductCode")
dr = dt.NewRow
dr("Items") = "1stItem"
dr("Qty") = 9
dr("ProductCode") = "sdf"
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Items") = "2ndItem"
dr("Qty") = 4
dr("ProductCode") = "poi"
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Items") = "3rdItem"
dr("Qty") = 7
dr("ProductCode") = "wer"
dt.Rows.Add(dr)
dr = dt.NewRow
dr("Items") = "4thItem"
dr("Qty") = 2
dr("ProductCode") = "mnb"
dt.Rows.Add(dr)
MakeFakeTestDataTable = dt
End Function
Last edited by thetimmer; Mar 20th, 2014 at 08:14 AM.
Reason: Forgot the data
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|