1,105,578 Community Members

how populate listview in vb.net

Member Avatar
khair.ullah
Junior Poster in Training
72 posts since Aug 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Begginnerdev
Veteran Poster
1,142 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
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))
Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

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
Member Avatar
Reverend Jim
Noli mentula
5,436 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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)}
Member Avatar
khair.ullah
Junior Poster in Training
72 posts since Aug 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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 .

Member Avatar
ryan311
Posting Whiz in Training
294 posts since Jul 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
khair.ullah
Junior Poster in Training
72 posts since Aug 2012
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks all

Member Avatar
Reverend Jim
Noli mentula
5,436 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
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})
Question Answered as of 1 Year Ago by Reverend Jim, Begginnerdev, TnTinMN and 1 other
evadehawkeye07
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: