Question ListView Load Item

hisheeraz

Member
Joined
Oct 5, 2008
Messages
11
Programming Experience
1-3
hey guys
i am having trouble loading data into listview from access database

VB.NET:
Try
            Dim iForLoop As Integer
            Dim ds As DataSet
            Dim da As System.Data.OleDb.OleDbDataAdapter

            sql = "SELECT * FROM tblRoster"
            da = New OleDb.OleDbDataAdapter(sql, con)
            ds = New DataSet
            da.Fill(ds, "tblRoster")
            sqlR_Count = ds.Tables(0).Rows.Count

            For iForLoop = 1 To sqlR_Count-1
                Dim new_item = New ListViewItem
                'im getting error on below inw
                new_item = (ds.Tables("tblRoster").Rows(iForLoop)("eDate"))

                lvViewRoster.Items.Add(new_item)
            Next iForLoop
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

here is the error im getting

VB.NET:
Unable to cast object of type 'System.String' to type 'System.Windows.Forms.ListViewItem'.

any help would be much appreciated.
thanks

shiraz
 
Hi There,

This is one i used might be some help for you?

VB.NET:
Imports System.Data.OleDb

Public Class Form1
    Dim dt As New DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                      "Data Source=" & Application.StartupPath & "\League.mdb")

        Dim da As New OleDbDataAdapter("SELECT * FROM League Order By Points Desc, [Goal Diff] Desc", cn)
        da.Fill(dt)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Add the headers
        ListView1.Columns.Add("      Team", 200)
        ListView1.Columns.Add("Played")
        ListView1.Columns.Add("Won")
        ListView1.Columns.Add("Drawn")
        ListView1.Columns.Add("Lost")
        ListView1.Columns.Add("Goal Diff")
        ListView1.Columns.Add("Points")
        ListView1.CheckBoxes = True

        For Each dr As DataRow In dt.Rows
            Dim lvi As New ListViewItem(dr(0).ToString)
            With lvi
                lvi.SubItems.Add(dr(1).ToString)
                lvi.SubItems.Add(dr(2).ToString)
                lvi.SubItems.Add(dr(3).ToString)
                lvi.SubItems.Add(dr(4).ToString)
                lvi.SubItems.Add(dr(5).ToString)
                lvi.SubItems.Add(dr(6).ToString)
                ListView1.Items.Add(lvi)
               
            End With

        Next
    End Sub
End Class

It was used to help me change all checked boxes to checked if un-checked it is a list of string (teams) and integers (scores) and shows them all in a listview box i had to change the View mode properties to Details though on the listview.
 
Back
Top