We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.

6
Contributors
8
Replies
1 Week
Discussion Span
4 Months Ago
Last Updated
21
Views
Question
Answered
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
Moderator
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

Are you using STORED PROCEDURE?

If you are using then

        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim numberrow As DataRow
        objCon = New SqlConnection(conStr) <- Just replace this to your connection string.
        objCon.Open()
        Dim cmd As New SqlCommand("sp_Customer_Process", objCon)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("id", "")
        cmd.Parameters.AddWithValue("event", "load")
        cmd.Parameters.AddWithValue("name", "")
        cmd.Parameters.AddWithValue("address", "")
        cmd.Parameters.AddWithValue("contact", "")
        cmd.Parameters.AddWithValue("emailadd", "")
        cmd.Parameters.AddWithValue("search", searchword)
        cmd.ExecuteNonQuery()

        da.SelectCommand = cmd
        da.Fill(ds, "load")
        dt = ds.Tables("load")

        ListView1.Items.Clear()

        For Each numberrow In ds.Tables("load").Rows
            ListView1.Items.Add(New ListViewItem(numberrow.ItemArray.GetValue(0).ToString.ToUpper))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(numberrow.ItemArray.GetValue(1).ToString.ToUpper)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(numberrow.ItemArray.GetValue(2).ToString.ToUpper)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(numberrow.ItemArray.GetValue(3).ToString.ToUpper)
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(numberrow.ItemArray.GetValue(4).ToString.ToUpper)
        Next
ryan311
Posting Whiz in Training
294 posts since Jul 2008
Reputation Points: 3
Solved Threads: 6
Skill Endorsements: 0

thanks all

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
Moderator
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

the last item in the databases is not showing in the listview? i don't know what's wrong.

evadehawkeye07
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0802 seconds using 2.7MB