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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding Data to a ListView in .net 2003

Status
Not open for further replies.

angel106

Technical User
Aug 1, 2005
39
0
0
US
How can i bind the data to a list view control? I have created a sqlconnection and configured the sqldataadapter to create a dataset, however how can i bind that data set so that when te form loads the data from my database is in the listview?
 
You could loop through the records in the dataset using a datarow and create a new listviewitem for each record:

Code:
    Dim dSet As DataSet
    Dim dRow As DataRow
    Dim itemX As ListViewItem

    For Each dRow In dSet.Tables("myTable").Rows
      itemX = ListView1.Items.Add(dRow("myField1"))
      itemX.SubItems.Add(dRow("myField2"))
      itemX.SubItems.Add(dRow("myField3"))
      'and so on...
    Next

For each new record you can use itemX to set it's subitems. But you can also set other properties of a single row in your listview this way. For example, I often use a boolean to create alternating background colors for the rows. This looks really neat:

Code:
    Dim dSet As DataSet
    Dim dRow As DataRow
    Dim itemX As ListViewItem
    Dim bColor As Boolean

    For Each dRow In dSet.Tables("myTable").Rows
      itemX = ListView1.Items.Add(dRow("myField1"))
      itemX.SubItems.Add(dRow("myField2"))
      itemX.SubItems.Add(dRow("myField3"))
      'and so on...
      If bColor Then
        itemX.BackColor = Color.White
      Else
        itemX.BackColor = Color.AliceBlue
      End If
      bColor = Not bColor
    Next

The code is very incomplete, you have to (of course) use your own dataset, already filled with data and you have to define the columns for the listview properly, either at design or run time.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Ok so i created an array list with all the database data now how can i display if i want to use a listview from the fired event of a button pushed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top