Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Popultaing a ListView

Status
Not open for further replies.

tc3596

Technical User
Mar 16, 2001
283
0
0
I have read many articles on this and nothing seems to work...Here is my code..

Dim myConnection As New SqlConnection
myConnection = New SqlConnection("server=localhost;database=TestDB;Trusted_Connection=Yes;")
Dim myCommand As New SqlCommand("Select Top 100 SNR_Id,Ord_no, Convert(varchar, Rel_no) AS Rel_No, Pallet_no,Item_no,UOM,Ser_lot_no,Convert(varchar,Ser_lot_qty) as Ser_lot_qty,Convert(varchar, Exp_dt) as exp_dt,Convert(varchar, Request_dt) as request_dt,Compl_fg From ShipNotification Order By Id Desc", myConnection)


Try
myConnection.Open()
Dim dr As SqlDataReader = myCommand.ExecuteReader()


ListView1.Columns.Add(("SNR No."), 150, HorizontalAlignment.Left)
ListView1.Columns.Add(("Ord_No"), 150, HorizontalAlignment.Right)
ListView1.Columns.Add(("Rel_No"), 150, HorizontalAlignment.Right)
ListView1.Columns.Add(("Pallet_no"), 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Item_no", 500, HorizontalAlignment.Left)
ListView1.Columns.Add("UOM", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Ser_lot_no", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Ser_lot_qty", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Exp_Dt", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Request_Dt", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Comp_Fg", 150, HorizontalAlignment.Left)



ListView1.Items.Clear()

Do While dr.Read
ListView1.Items.Clear()
Dim itmListItem = New ListViewItem()

For i As Integer = 0 To dr.FieldCount() - 1

ListView1.Items.Add(dr.GetString(i))


Next i

Loop



dr.Close()
myConnection.Close()



Catch ex As Exception
End Try



...this produces all of the data on screen...however, it all runs into each other (1 row with 22 cols). There are 2 records in the query with 11 columns each. I essentially get 22 columns. I have tried using a listview item...but never could get subitems.add to populate anything. I would use a listviewitem.text = dr(0) on each loop, but that would be the only values displaying and once again they would not be on different rows. What am I missing?

Tim
 
Code:
               Dim nFields As Integer = dr.FieldCount

'        For i As Integer = 0 To nFields - 1
'            ListView1.Columns.Add(dr.GetName(i), 100, HorizontalAlignment.Left)
'        Next i


        Dim nRow As Integer = 0
        Do While dr.Read()
            Dim subitems(nFields - 1) As String
            For i As Integer = 0 To nFields - 1
                subitems(i) = dr(i).ToString()
            Next i
            Dim item As New ListViewItem(subitems, -1)
            ListView1.Items.Add(item)
            nRow += 1
        Loop

Zameer Abdulla
 
Thanks for your response. My data was not displaying correctly because I needed to set the view property to details. I can't believe this isn't a default setting. I like the loop of the getname property. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top