• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

problem with listview in vb.net ----- help ----

hey all i have just started using vb.net after using vb6 and am trying to get data from a access database to put in a listview box but i keep on gettting {"Unable to cast object of type 'System.String' to type 'ListViewSubItem'."} when i try pulling numbers from the database

  rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
        Do Until rs.EOF
            objlst = search.find.Items.Add(rs.Fields("description").Value)
            objlst.SubItems(1) = rs.Fields("stockid").Value

that is my code
0
awolarczuk
Asked:
awolarczuk
  • 14
  • 13
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Hi awolarczuk,

Try this....

    objlst.SubItems(1).Text = rs.Fields("stockid").Value

Regards,

Wayne
0
 
Wayne Taylor (webtubbs)Commented:
BTW - I still recommend you move away from ADO to ADO.Net -> http://msdn2.microsoft.com/en-us/library/h43ks021(VS.71).aspx
0
 
awolarczukAuthor Commented:
yea i will i am  doing some demo stuff with it atm but i am just using what i thought i knew to get this done
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
awolarczukAuthor Commented:
ok now i get InvalidArgument=Value of '1' is not valid for 'index'.
0
 
Wayne Taylor (webtubbs)Commented:
Ahh, yeah, you need to add the SubItem first....

            objlst = search.find.Items.Add(rs.Fields("description").Value)
            objlst.SubItems.Add(rs.Fields("stockid").Value)

Wayne
0
 
Wayne Taylor (webtubbs)Commented:
Actually, if your ListView is called 'search', you can simply use this....

            objlst = search.Items.Add(rs.Fields("description").Value)
            objlst.SubItems.Add(rs.Fields("stockid").Value)
0
 
awolarczukAuthor Commented:
ok i am not getting  any errors but i am not getting thing showing in the listview now
0
 
Wayne Taylor (webtubbs)Commented:
Is the item being added?

SubItems will only be visible when the View property of the listview is set to "Details"
0
 
awolarczukAuthor Commented:
ok when i do a debug i see the data in that window but i have the listview displayed as detail i get no infomation in it as list i get the first col but not the 2nd, any ideas
0
 
awolarczukAuthor Commented:
when it is set to details i dont see anything??
0
 
Wayne Taylor (webtubbs)Commented:
You'll also need to add the columns to your listview before any subitems will show. This can be done in the Columns property of the listview.

Wayne
0
 
awolarczukAuthor Commented:
i dont even see the titles of the colu
0
 
awolarczukAuthor Commented:
i have already done that i have two of them, that is basicly the same as in vb6
0
 
Wayne Taylor (webtubbs)Commented:
OK, let me summarize....

To show subitems, the following things need to be done....

1. Set the View property of the listview to "View"
2. Add your columns to the listview's Columns property
3. Use code like this....

    Dim lvi As ListViewItem = ListView1.Items.Add("Item1")
    lvi.SubItems.Add("SubItem1")

By ensuring all the above are done, the subitems will display.
0
 
awolarczukAuthor Commented:
ok 1. there is no "view" option in the view property here is on details, which it is set to now
2. this is my current code which is set like your example
0
 
Wayne Taylor (webtubbs)Commented:
Sorry, it should have been this....

1. Set the View property of the listview to "Details"

Can you try creating a new project, with a simple form, a listview control (with View property set to Details, and 2 columns added), and this code...

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lvi As ListViewItem = ListView1.Items.Add("Item1")
        lvi.SubItems.Add("SubItem1")
    End Sub

What happens?
0
 
awolarczukAuthor Commented:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        search.find.Clear()

        Dim rs As New ADODB.Recordset
        Dim sql As String
        Dim objlst As ListViewItem
        sql = "select * from [stock_card] where [description] like  '%" & Replace(Ldesc.Text, "'", "''") & "%'"

        Debug.Print(sql)
        rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
        Do Until rs.EOF
            objlst = search.find.Items.Add(rs.Fields("description").Value)
            MsgBox(rs.Fields("description").Value)

            objlst.SubItems.Add(rs.Fields("stockid").Value)
            MsgBox(rs.Fields("stockid").Value)
            search.Left = Me.Width + 95
            search.Top = Me.Top
            search.Show()
            rs.Update()
            rs.MoveNext()
        Loop
        rs.Close()

    End Sub
0
 
Wayne Taylor (webtubbs)Commented:
What is 'search.find'?
0
 
awolarczukAuthor Commented:
search is the form and find is what i have call my listview
0
 
Wayne Taylor (webtubbs)Commented:
So the View property is definitely set to Details and there are definitely 2 columns? If so, it should work. I can't see any problem with it.
0
 
Wayne Taylor (webtubbs)Commented:
Also, are there any results returned by the query?
0
 
awolarczukAuthor Commented:
yep there are i as you can see i have added a message to the results as well and the message comes up with the results, and if i put the listview in list i get the first col or results but not the 2nd
0
 
Wayne Taylor (webtubbs)Commented:
and there are definitely 2 columns added to the listview?
0
 
awolarczukAuthor Commented:
yep i have one called description and one call stockid, i thought that to have i have checked and then check again
0
 
Wayne Taylor (webtubbs)Commented:
Can you try creating a new project, with a simple form, a listview control (with View property set to Details, and 2 columns added), and this code...

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lvi As ListViewItem = ListView1.Items.Add("Item1")
        lvi.SubItems.Add("SubItem1")
    End Sub

What happens?
0
 
awolarczukAuthor Commented:
ok i got it working it was the clear that i had at the top of the code, i was hoping that you could help me with a last problem that i have when i try to do a query with vb.net

 sql = "select * from [stock_card] where [stockid] =  & "search.ListView1.SelectedItems(1)

that is what i normal use in vb6 but is does seem to want to work in .net how do i use it in .net

thanks
0
 
Wayne Taylor (webtubbs)Commented:
awolarczuk,

Because it's a separate to above, you should really be asking it in a separate question

Regards,

Wayne
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 14
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now