4 Replies - 476 Views - Last Post: 02 March 2013 - 10:15 AM Rate Topic: -----

#1 TejashreeGanesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

About Listview Control

Posted 02 March 2013 - 01:01 AM

Hi!,
Please tell me how can I add/enter text at run time in listview control (as sub items).
i.e. I have 4 columns in Listview Control
No. Item Qty Rate

when I select P.O. 1 from combo box then it displays No. Item and Qty in listview, I want to add Rate at runtime.
I could not enter the Rate in Rate column how can I do this?

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: About Listview Control

#2 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: About Listview Control

Posted 02 March 2013 - 02:12 AM

Hi,

Here's an example how to add subitems into Listview:

 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim strItemText As String
        Dim objItem As ListViewItem

        'Add some list view items.
        strItemText = "name:"
        objItem = ListView1.Items.Add(strItemText)
        With objItem
            .SubItems.Add("123 Some St.")
            .SubItems.Add("555-555-5555")
            .SubItems.Add("555-555-5555")
            .ImageIndex = 0
        End With

    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 251
  • View blog
  • Posts: 810
  • Joined: 01-February 13

Re: About Listview Control

Posted 02 March 2013 - 07:17 AM

Quote

when I select P.O. 1 from combo box then it displays No. Item and Qty in listview, I want to add Rate at runtime. I could not enter the Rate in Rate column how can I do this?


Are you adding several rows before adding the Rate or are you adding 1 row and then the Rate before adding the next row?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: About Listview Control

Posted 02 March 2013 - 08:35 AM

I'm going to make a huge assumption here. I'm assuming that when you populate the row from the combobox you are creating the subitems for No.Item and Qty but you are not at that time creating the subitem for Rate so even though the listview has that column, the listviewitem doesn't.

An easy way to alleviate this is where you are adding the .SubItems, include a .SubItem for the rate column with a simple empty string "" then you can easily just change that subitem by specifying it's indexes.

Something like

ListView1.Items(ListView1.FocusedItem.Index).SubItems(4).Text = Rate.ToString

Do not confuse the column with a subitem as you can create a list view with say
Column1, Column2, Column3

And when you add the ListViewItem (row) that row only has subitems that you add

If I were to then say
Dim lv as New ListViewITem
Lv.Text = "Column1Text"
Lv.SubItems.Add("Column2Text")
'Note I didn't add a subitem here for Column3 so it doesn't exist
ListView1.Items.Add(lv)


This would produce a row of
Column1    |Column2    |Column3
Column1Text Column2Text NOTHING


NOTHING meaning a SubItem was not added to that row
Therefore, I could not do
ListView1.Items(0).SubItems(2).Text = "Column3Text"

But if I would have added lv.SubItems.Add("") right before the ListView1.Items.Add(lv) it would have worked.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 251
  • View blog
  • Posts: 810
  • Joined: 01-February 13

Re: About Listview Control

Posted 02 March 2013 - 10:15 AM

CharlieMay has nailed what i was thinking in his above post. You need to add an empty string for the Rate subitem when you are adding the rest of the data in the row.

Being that i don`t know in what order you are adding rows and how you are selecting the row to set the Rate. I made this example to add the row(s) and show you a couple of ways to get the row index to add the Rate to.

    Private Sub Button_AddMyItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddMyItems.Click
        'Just For Example Of Whatever Your Strings Are
        Dim MyNumber As String = "My No."
        Dim MyItem As String = "My Item"
        Dim MyQuantity As String = "My Qty"
        Dim MyRate As String = "" 'empty string to add in Rate column

        Dim lvi As New ListViewItem
        lvi.Text = MyNumber
        lvi.SubItems.AddRange(New String() {MyItem, MyQuantity, MyRate})
        ListView1.Items.Add(lvi)
    End Sub

    Private Sub Button_AddMyRate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddMyRate.Click
        Dim MyRate As String = "My Rate"

        Dim TheRowIndex As Integer = 0

        'Use this if you are adding 1 row of data and then
        'setting the Rate before adding another row.
        TheRowIndex = ListView1.Items.Count - 1

        'Use this if you are adding several rows and then selecting the row
        'in the listview with the mouse that you want to add the Rate to.
        TheRowIndex = ListView1.FocusedItem.Index

        '3 is the column index
        ListView1.Items(TheRowIndex).SubItems(3).Text = MyRate
    End Sub


This post has been edited by IronRazer: 02 March 2013 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1