3 Replies - 8215 Views - Last Post: 06 September 2010 - 03:49 PM Rate Topic: -----

#1 qtex  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 142
  • Joined: 24-October 09

How to connect Listview with database

Posted 20 August 2010 - 10:29 AM

Hello,

I'm trying to connect listview with database. I don't know much about databases. I tryed to search some tutorial or something. Every tutorial what I looked had some error. So anyone know where to find a working tutorial ?

thanks,
Is This A Good Question/Topic? 0
  • +

Replies To: How to connect Listview with database

#2 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: How to connect Listview with database

Posted 20 August 2010 - 11:47 AM

if you have already looked at tutorials then are you sure they are the ones that have the errors? post some of the code you tried and where you get the error and we could probably help you alot easier.
Was This Post Helpful? 0
  • +
  • -

#3 .:Deco:.  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-August 10

Re: How to connect Listview with database

Posted 20 August 2010 - 11:51 AM

You can also use a DataGridView for that instead of a listview... works just about the same
Was This Post Helpful? 0
  • +
  • -

#4 emresutisna  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 80
  • Joined: 14-May 09

Re: How to connect Listview with database

Posted 06 September 2010 - 03:49 PM

To display the datas from database to Listview, you can try this code below. At this ex I use database named "Northwind" which has a table called "Customers" and fields like "CustomerID" and "CompanyName". Actually you can download this database from Microsoft web.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  'Adding columns of ListView1
  With ListView1.Columns
    .Add("Customer ID", 100) 'The text of column header and the column width
    .Add("Company Name", 100)
  End With
  'Connecting to database and stored the data into dataset
  Using con As New SqlClient.SqlConnection
     con.ConnectionString = "Data Source=localhost; Initial Catalog=Northwind; Integrated Security = True"
     Using cmd As New SqlClient.SqlCommand
        cmd.CommandText = "Select * From Customers"
        cmd.Connection = con
        con.Open()
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "tbl_Customer")
        Dim bs As New BindingSource
        bs.DataSource = ds
        bs.DataMember = "tbl_Customer"
        'Adding items of ListView1
        With ListView1
           For i As Integer = 0 To bs.Count - 1
              Dim lst As New ListViewItem
              With lst
                .Text = bs.Current("CustomerID").ToString
                .SubItems.Add(bs.Current("CompanyName").ToString)
              End With
             .Items.Add(lst)
             bs.MoveNext()
           Next
        End With
    End Using
  End Using
End Sub



Hope it helps you.
Don't forget to click "+" button on the bottom of my post if it's helpful :smile2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1