By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

How Do I Add Items and SubItems to a Selected ListView Row?

P: 6
Hello,

In my application I have a form with a ListView control. I have three columns and I add text to each of those columns from three text boxes using a single Click Event.

This works fine as long as I want to append the new items and subitems to the last line of my listView control. However, what I need sometimes is to edit the items and subitems at a specific row in the list.

For example, I would like to select a certain row, add the items and subitems from the text boxes and have it add those items to that line, not the last line.

My guess is that it could be done by identifying the index of a particular row and sending the items and subitems to that row based on the index. I expect that if there is already some item or subitem at that location, adding a new one will overwrite the existing one.

I know how to select a row and determine the index but I cannot figure out how to add the items to a row of that particular index.

Can anyone help me?


This is my code:


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItems.Click
  4.  
  5.         Dim strItem As String
  6.         strItem = TextBox.Text
  7.  
  8.         Dim strItem1 As String
  9.         strItem1 = TextBox1.Text
  10.  
  11.         Dim strItem2 As String
  12.         strItem2 = TextBox2.Text
  13.  
  14.  
  15.      Dim item1 As New ListViewItem(strItem)
  16.      ListView1.Items.AddRange(New ListViewItem() {item1})
  17.      item1.SubItems.Add(strItem1)
  18.      item1.SubItems.Add(strItem2)
  19.  
  20.  
  21.         TextBox.Clear()
  22.         TextBox1.Clear()
  23.         TextBox2.Clear()
  24.  
  25.     End Sub
  26.  
  27.  
  28.  
  29.  
  30.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.         ListView1.GridLines = True
  32.         ListView1.View = View.Details
  33.  
  34.  
  35.         ListView1.Columns.Add("Item Column", 150)
  36.         ListView1.Columns.Add("Column 2", 100)
  37.         ListView1.Columns.Add("Column 3", 100)
  38.         ListView1.Font = New Font("times", 14)
  39.         ListView1.FullRowSelect = True
  40.  
  41.     End Sub
  42.     Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
  43.         'Inserts selected ListView Item to the designated text box when the listview item is selected
  44.         If ListView1.SelectedItems.Count > 0 Then
  45.  
  46.             Dim selectedItem As String
  47.             selectedItem = ListView1.SelectedItems(0).Text
  48.             TextBox.Text = selectedItem
  49.         End If
  50.  
  51.         Dim subitem1 As String
  52.         subitem1 = ListView1.SelectedItems(0).SubItems(1).Text()
  53.         TextBox1.Text = subitem1
  54.  
  55.         Dim subitem2 As String
  56.         subitem2 = ListView1.SelectedItems(0).SubItems(2).Text()
  57.         TextBox2.Text = subitem2
  58.  
  59.         End Sub
  60.  
  61.  
  62.  
  63.  
  64.  
  65.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  66.  
  67.         Dim index As Integer = ListView1.SelectedIndices(0)
  68.         txtIndex.text = index
  69.  
  70.     End Sub
  71. End Class
  72.  
Dec 24 '12 #1

✓ answered by CyberSoftHari

UiListitems-> user interface listitems
strDisplayText-> text to display in list items.
intInex->row index(number)
intFieldIndex->column index(number)
strKEY->key value for row

Share this Question
Share on Google+
4 Replies


PsychoCoder
Expert Mod 100+
P: 465
This is VB.NET so I'll go a head and move this to the VB.NET forum for you so it can be seen by the proper experts :)
Dec 25 '12 #2

Expert 100+
P: 487
Expand|Select|Wrap|Line Numbers
  1. Dim tmpListItem As ListItem ' Declaration should be out of add list function
  2. Set tmpListItem  = UIlstItem.ListItems.Add(intIndex,strKey ,strDisplayText)
  3. tmpListItem.SubItems(fieldIndex) = strDisplayString2 
  4.  
Above code format is an vb6 code and you can try this for VB.Net
Dec 25 '12 #3

P: 6
@CyberSoftHari
Hello. Thank you for replying. I do not fully follow your code. Please explain the meaning of intIndex, strKey, and strDisplayText.

Also, could you explain "fieldIndex and strDisplayString2? Thank you.
Dec 27 '12 #4

Expert 100+
P: 487
UiListitems-> user interface listitems
strDisplayText-> text to display in list items.
intInex->row index(number)
intFieldIndex->column index(number)
strKEY->key value for row
Dec 28 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.