Find a specific ListView Item

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hi,

I have a ComboBox that is bound to a DataTable. How do I use the SelectedValue of this ComboBox to locate and focus on the item in a ListView?

Cheers,

sfx
 
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'load data into the dataset
        Me.DataSet11.Employees.Clear()
        Me.OleDbDataAdapter1.Fill(Me.DataSet11.Employees)

        'create the column of the listview at runtime
        For Each col As DataColumn In Me.DataSet11.Employees.Columns
            Dim listColumn As New ColumnHeader
            listColumn.Text = col.ColumnName
            Me.ListView1.Columns.Add(listColumn)
        Next

        'populate the name column in the employee datatable
        'fill the litview with the records in the datatable
        For Each row As DataSet1.EmployeesRow In Me.DataSet11.Employees.Rows
            row.Name = row.FirstName.Trim & " " & row.LastName.Trim


            Dim count As Integer = Me.DataSet11.Employees.Columns.Count
            Dim str(count) As String

            For i As Integer = 0 To count - 1
                If Not IsDBNull(row(i)) Then str(i) = CStr(row(i))
            Next
            Me.ListView1.Items.Add(New ListViewItem(str))
        Next

        'select the first item in the list as the name from the first record will be displayed on form load
        Me.ListView1.Items(0).Selected = True
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Me.ListView1.Items(Me.ComboBox1.SelectedIndex).Selected = True
    End Sub



Hope the above code helps. The form_load event databinds the combobox and the combobox_selected event locates and focuses the corresponding record in the listview.
 
Back
Top