how populate listview in vb.net
hi all.
I want to fill the Listview from a mssql database in vb.net but it not show all the data in Listview. I have the followning code.
Dim sSQL As String Dim lvwItem As New ListViewItem()
sSQL = "SELECT field1, field2, field3 FROM tablenam e "
sSQL = sSQL & "ORDER BY field1 " Dim command As New SqlCommand (sSQL, myConne ction)
Dim reader As SqlDataR eader = command .Execute Reader()
While reader.Read()
lvwItem = ListView 1. Items. Add (reader.G etString (0))
lvwItem. SubItem s .Add (reader.G etString (1))
lvwItem. SubItem s .Add (reader.G etString (2))
End While
End Sub
it only show the first column data of database in Row . . . And not show any else. . Plz help me what problem in my code.
Related Article: Read XML with VB,NET
is a solved VB.NET discussion thread by buffalo0 that has 3 replies, was last updated 4 months ago and has been tagged with the keywords: .net, asp.net, vb.net, xml.
khair.ullah
Junior Poster in Training
57 posts since Aug 2012
Reputation Points: -2
Solved Threads: 3
Skill Endorsements: 0
The first column in a listview hold's the value placed in the LVI's .Text property.
Example:
|Record| Name | Age | Date |
|.Text |SubItem|SubItem|SubItem|
This being said - you need to change your code to reflect this:
lvwItem = ListView 1. Items. Add (reader.G etString (0))
To:
lvwItem.Text = reader.GetString(0)
lvwItem.SubItems.Add(reader.GetString(1))
lvwItem.SubItems.Add(reader.GetString(2))
Begginnerdev
Practically a Posting Shark
892 posts since Apr 2010
Reputation Points: 198
Solved Threads: 149
Skill Endorsements: 9
If you are only showing the first column, then I suspect you may not have the ListView configured correctly. I don't use this control very often, but for three columns, I would do it like this:
With ListView1
.View = View.Details
.HeaderStyle = ColumnHeaderStyle.None ' set to whatever you need
.Columns.Clear() ' make sure collumnscollection is empty
' Add 3 columns
.Columns.AddRange(New ColumnHeader() {New ColumnHeader(), New ColumnHeader(), New ColumnHeader()})
End With
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 149
Skill Endorsements: 13
You can also add an entire row to a listview (in details view) in one step by
ListView1.Items.Add(New ListViewItem({"col1data","col2data","col3data"}))
In your case the parameter(s) would be
{reader.GetString(0),reader.GetString(1),reader.GetString(2)}
Reverend Jim
Illigitimae non carborundum
3,737 posts since Aug 2010
Reputation Points: 585
Solved Threads: 469
Skill Endorsements: 33
thanks all
what will be the complete code for listview if i want to fill the ListView from Ms Sql server database when the form Load .
khair.ullah
Junior Poster in Training
57 posts since Aug 2012
Reputation Points: -2
Solved Threads: 3
Skill Endorsements: 0
khair.ullah
Junior Poster in Training
57 posts since Aug 2012
Reputation Points: -2
Solved Threads: 3
Skill Endorsements: 0
Inside the loop you can save some typing by
ListView1.Items.Add(New ListViewItem({ _
numberrow.ItemArray.GetValue(0).ToString.ToUpper, _
numberrow.ItemArray.GetValue(1).ToString.ToUpper, _
numberrow.ItemArray.GetValue(2).ToString.ToUpper, _
numberrow.ItemArray.GetValue(3).ToString.ToUpper, _
numberrow.ItemArray.GetValue(4).ToString.ToUpper})
Reverend Jim
Illigitimae non carborundum
3,737 posts since Aug 2010
Reputation Points: 585
Solved Threads: 469
Skill Endorsements: 33
Question Answered as of 4 Months Ago by
Reverend Jim,
Begginnerdev,
TnTinMN
and 1 other