Hi! Can someone tell me on how could I display the data from my SQL to a ListView? I only have one column to display. Thank you very much and God Bless! :)

here is a sample program for what u want to do.. i have used the NorthWind Database. u can use yours.

Complete Coding:

Imports System.Data.SqlClient
Public Class Form1
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim ds As DataSet
    Dim itemcoll(100) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.ListView1.View = View.Details
        Me.ListView1.GridLines = True

        conn = New SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=id;Password=pass")

        Dim strQ As String = String.Empty

        strQ = "SELECT * FROM Northwind.dbo.Products"

        cmd = New SqlCommand(strQ, conn)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "Table")

        Dim i As Integer = 0
        Dim j As Integer = 0

        ' adding the columns in ListView
        For i = 0 To ds.Tables(0).Columns.Count - 1

        'Now adding the Items in Listview
        For i = 0 To ds.Tables(0).Rows.Count - 1

            For j = 0 To ds.Tables(0).Columns.Count - 1

                itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()

            Dim lvi As New ListViewItem(itemcoll)

    End Sub
End Class

Thanks! I did it with this:


        Dim qstn As String
        Dim cmd As New OleDbCommand("select Question from Questions", mycurrentconnection)
        cmd.CommandType = CommandType.Text

        Dim reader As OleDbDataReader = cmd.ExecuteReader()

        While reader.Read()
            qstn = reader("Question").ToString

        End While


my only problem is, my column name doesn't show. :)


add above line before the while loop. it will add a column name Question

listView1.View = View.list - this works but displays no column name.
listView1.View = View.Details - I think this is appropriate but it does not work on mine. :(

Sorry, but how do you attach photos here? :)

go to Adavanced Editor and Find Attachment there.. its a UPin Symbol

I would like to add my Column Name "Questions" there. :)

I would like my listview to be like yours. However, if I set my listview view to details, it shows nothing.

if u are only having one column then..add a column manually from Design View in Visual Studio.

here a is a snap of how to do it

just change to name property to what ever name u like..

Thank you very much! You are a big help! God Bless. :)