Results 1 to 6 of 6

Thread: ListView control and binding

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    ListView control and binding

    hi, first time back in a while

    i've been looking at the listview members and didn't find one to bind it to a datasource. Am I wrong in assuming you have to do this manually? I've written methods to take a datareader and fill a listview but would really like to bind automatically.

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: ListView control and binding

    Originally posted by Andy
    hi, first time back in a while

    i've been looking at the listview members and didn't find one to bind it to a datasource. Am I wrong in assuming you have to do this manually? I've written methods to take a datareader and fill a listview but would really like to bind automatically.
    yup i think listview has no properties like datagrid that can bind automatically. You must do it manually using datareader..
    but it quiet good using that thing.
    VB Code:
    1. dim dt as new datatable()
    2. dim da as new sqldataadapter("select * from sample table",cn)
    3. da.fill(dt)
    4. datagrid1.datasource=dt

    hey andy where is your little baby?
    i havent seen it putting in your avatar..

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: Re: ListView control and binding

    Originally posted by mar_zim
    hey andy where is your little baby?
    i havent seen it putting in your avatar..
    i guess it's turning one and a half year now.

    anywayz, we could create our listview with a property datasource
    VB Code:
    1. Option Strict On
    2.  
    3. Public Class xlistview
    4.     Inherits ListView
    5.  
    6.     Sub New()
    7.         Me.View = View.Details
    8.         Me.FullRowSelect = True
    9.     End Sub
    10.  
    11.     Dim m_datasource As DataTable
    12.     Public Property datasource() As DataTable
    13.         Get
    14.             Return m_datasource
    15.         End Get
    16.         Set(ByVal Value As DataTable)
    17.             m_datasource = Value
    18.             If Not m_datasource Is Nothing Then
    19.                 initcomponents()
    20.             End If
    21.         End Set
    22.     End Property
    23.  
    24.     Sub initcomponents()
    25.         Dim c As DataColumn
    26.         For Each c In m_datasource.Columns
    27.             Dim ch As New ColumnHeader()
    28.             ch.Text = c.ColumnName
    29.             Me.Columns.Add(ch)
    30.         Next
    31.  
    32.         Dim r As DataRow
    33.         For Each r In m_datasource.Rows
    34.             Dim li As ListViewItem = Me.Items.Add(r(0).ToString)
    35.             Dim i As Integer
    36.             For i = 1 To m_datasource.Columns.Count - 1
    37.                 li.SubItems.Add(r(i).ToString)
    38.             Next
    39.         Next
    40.     End Sub
    41. End Class
    not a good coding though. pardon me mates.

    sample use
    VB Code:
    1. Dim cn As New SqlConnection("user id=sa;password=password;initial catalog=northwind")
    2.     Dim da As New SqlDataAdapter()
    3.     Dim dt As New DataTable()
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         da.SelectCommand = New SqlCommand("select * from territories", cn)
    6.         da.Fill(dt)
    7.  
    8.         Xlistview1.datasource = dt
    9.     End Sub
    hope this helps

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    wow guys! i didn't know anyone would remember my little girls' picture I'll have to create a new avatar and post it.

    Thanks for the suggestions too. I'm going to try those code snippets right now

    and, yes, my little girl is almost 1 1/2 years old

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    gee, i told ya zim, one and a half. hahaha. i'm telepathic.

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    Originally posted by Andy
    wow guys! i didn't know anyone would remember my little girls' picture I'll have to create a new avatar and post it.

    Thanks for the suggestions too. I'm going to try those code snippets right now

    and, yes, my little girl is almost 1 1/2 years old
    yeah we recognize your cute little girl..

    i hope you can generate a dozen of little baby so that you can lend your skills and (mana) to your children

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width