putting data from an access database using list view

liam

Member
Joined
Jun 8, 2004
Messages
23
Programming Experience
Beginner
I'm using list view to display data coming from an Access database. Haven't tried it though. Anybody who could unburden me with thinking what to do? Thanks.
 

Radhakrishnan

Member
Joined
Jun 3, 2004
Messages
17
Location
Chennai - India
Programming Experience
5-10
Hi try this

assume that your dataset name is ds

lstView1.Clear()
lstView1.View = View.Details
lstView1.Columns.Add("c1", Label23.Width, HorizontalAlignment.Left)
lstView1.Columns.Add("c2", Label24.Width, HorizontalAlignment.Center)
lstView1.Columns.Add("c3", Label25.Width, HorizontalAlignment.Right)
lstView1.Columns.Add("c4", Label26.Width, HorizontalAlignment.Right)
lstView1.Columns.Add("c5", Label27.Width, HorizontalAlignment.Right)
Dim DRow As DataRow
Dim i As Integer = 0
For Each DRow In ds.Tables(0).Rows
Dim item1 As New ListViewItem()
Dim sitem1 As New ListViewItem.ListViewSubItem()
Dim sitem2 As New ListViewItem.ListViewSubItem()
Dim sitem3 As New ListViewItem.ListViewSubItem()
Dim sitem4 As New ListViewItem.ListViewSubItem()
item1.Text = CStr(ds.Tables(0).Rows(i).Item(7))
sitem1.Text = CStr(ds.Tables(0).Rows(i).Item(6))
sitem2.Text = CStr(ds.Tables(0).Rows(i).Item(9))
sitem3.Text = CStr(ds.Tables(0).Rows(i).Item(10))
sitem4.Text = CStr(ds.Tables(0).Rows(i).Item(8))
item1.SubItems.Add(sitem1)
item1.SubItems.Add(sitem2)
item1.SubItems.Add(sitem3)
item1.SubItems.Add(sitem4)
lstView1.Items.Add(item1)
i = i + 1
Next
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Let me get this stright, you want to read records in an access database and add each record to the listview?

I have done something very similar so I could help you, if this is what you intend.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Right this is quite long but it basically does what you want along with a few other things. You will need to remove parts of the code because they are not needed for what you want.

Private Sub searchTitle()
'declare a new sqlcommand
Dim command As SqlCommand = New SqlCommand
'set the command type to text
command.CommandType = CommandType.Text
'specify the connection to use
command.Connection = SqlConnection1
'declare the SQL statement to use in the command text
command.CommandText = "SELECT pictureID, pictureTitle, pictureDescription, pictureOwner, pictureData FROM dbo.Pictures WHERE (pictureTitle LIKE @pictureTitle)"
command.Parameters.Add(New SqlParameter("@pictureTitle", SqlDbType.VarChar))
command.Parameters("@pictureTitle").Value = "%" + TextBoxTitle.Text + "%"
Try
'clear listviewleft
ListViewLeft.Items.Clear()
'open connection
command.Connection.Open()
'declare sqldataread
Dim dr As SqlDataReader
dr = command.ExecuteReader(CommandBehavior.Default)
'declare a variable of class picture
Dim picture As picture

While dr.Read
'create a new picture
picture = New picture
'assaign the relevant information to each part of picture
picture.pictureTitle = dr.GetValue(1)
'read a memorystream of bytes fromt he database and put it into the pictureData variable
picture.pictureData = New System.IO.MemoryStream(dr("pictureData"), True)
picture.pictureDescription = dr.GetValue(2)
picture.pictureID = dr.GetValue(0)
'declare a new listviewitem
Dim i As ListViewItem = New ListViewItem
'set the TAG of the new listviewitem to be equal to picture
i.Tag = picture
'set the TEXT of the new listviewitem to be equal to picture.pictureTitle
i.Text = picture.pictureTitle
'add the new listviewitem to listviewleft
ListViewLeft.Items.Add(i)
End While
Catch ex As Exception
StatusBar.Text = "Status: " & ex.Message
Finally
'close connection
command.Connection.Close()
'update status bar with how many files were found
StatusBar.Text = "Status: " & ListViewLeft.Items.Count & " files found"
End Try
End Sub
;)
 

liam

Member
Joined
Jun 8, 2004
Messages
23
Programming Experience
Beginner
Thanks a lot! Hope I could be help if anytime you too have questions. Cheers! Are you working for a company or a freelance software developer?
 
Top Bottom