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

Databinding a listview 1

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

VB 2005

I am having trouble databinding to a listview.

I thought data binding a listview was simple.

I have a dataset with a table i would like to bind to it. Columns are customerID, Name, address.

After this has been binded, how would I hide the customerID column, will still need this column as I want to perform some operations on the customer, but don't need to show the customerID number,

Many thanks in advance,

Steve
 
steve1rm,

A ListView doesn't support DataBinding unfortunately, so you have a couple options:
1.) Use a DataGrid/DataGridView and mimic the actions of a ListView control.
2.) Subclass the ListView and implement databinding yourself.

Here is an excellent article on databind a ListView Control.

Hope this at least points you in the right direction.


Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thanks for the information,

Its a shame it doesn't. I just have a small amount of data and rather then use the datagridview, i thought it would be good to use the listview.

Thanks

Steve
 
What database you are using?

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Usually I work with MS Access. I have a sample of it. Got it from the internet and modified for my usage.
Code:
    Dim con As OleDb.OleDbConnection
    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Zameer\MyDBs\ledgerdb.mdb"

    Private Sub btnAllTransactions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllTransactions.Click
        con = New OleDb.OleDbConnection(connectionString)
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter, dt As DataTable
        Dim dr As DataRow
        Dim sql As String
        sql = "SELECT * FROM tblTransactions ORDER BY TransDate"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds)
        With ListView1
            .Items.Clear()
            .View = View.Details
            .GridLines = True
            .FullRowSelect = True
            .Columns.Add("ID", 0, HorizontalAlignment.Left)
            .Columns.Add("Date", 100, HorizontalAlignment.Left)
            .Columns.Add("Transaction", 500, HorizontalAlignment.Left)
            .Columns.Add("Amount In", 100, HorizontalAlignment.Right)
            .Columns.Add("Amount Out", 100, HorizontalAlignment.Right)
        End With
        'Repeat for each table in the DataSet collection.
        For Each dt In ds.Tables
            'Repeat for each row in the table.
            For Each dr In dt.Rows
                Dim listItem As New ListViewItem(dr("RecID").ToString)
                listItem.SubItems.Add(Format(dr("TransDate"), "dd MMM yyyy"))
                listItem.SubItems.Add(dr("TransDescription"))
                listItem.SubItems.Add(Format(dr("AmountIn"), "##,##0.00"))
                listItem.SubItems.Add(Format(dr("AmountOut"), "##,##0.00"))
                ListView1.Items.Add(listItem)
            Next
        Next
    End Sub
Here is one for SQL Server2000
You can set the first column width to Zero as I have done in my example.
Then to ge the ID Number I use this code. credit goes to Planet-source-code.com
Code:
    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        'get listviews selected index
        If Me.ListView1.SelectedIndices.Count <= 0 Then
            Return
        End If
        Dim selNdx = Me.ListView1.SelectedIndices(0)
        If selNdx >= 0 Then
            MsgBox(ListView1.Items(selNdx).Text)
        End If
    End Sub
Hope this helps.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top