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.

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

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

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)}

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 .

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

thanks all

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})

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.