You display the records in a ListBox
ListBox control has a "DataSource" property, inyou case, you seta dataset asthe datasource of the listbox.
Listbox controlhas "DisplayMember" and "Value Member" fields. DisplayMember field specify the column name to display and the ValueMember field specify the value of the field. Most of the timeID fileds are assigned as the value member field. Whenan item of the listbox is clicked you can get the "selectedValue" and you can do whateever youwant to do with it, you can create new command, or you can get another columns vakue buusing select method and so on..
Check out this sample. Open a new windows form, open the code page, delete everything and paste the code shown below and run it..
Public
Class Form1
Dim dtStudents As DataTable
Friend WithEvents lstStudents As ListBox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 1st replace the listbox on the form
Me.lstStudents = New ListBox
Me.lstStudents.Width = Me.Width / 2
Me.lstStudents.Height = Me.Height
Me.lstStudents.Location = New Point(0, 0)
Me.Controls.Add(lstStudents)
' 2nd call the sub to create the datatable
CreateDataTable()
' 3rd add the datatable as the datasource of the listbox
Me.lstStudents.DisplayMember = "Name"
Me.lstStudents.ValueMember = "Id"
Me.lstStudents.DataSource = Me.dtStudents
End Sub
''' <summary>
''' this sub will create the data table and add new records in the table
''' </summary>
''' <remarks></remarks>
Public Sub CreateDataTable()
dtStudents = New DataTable
Dim clId As New DataColumn("Id")
clId.DataType = GetType(Integer)
Dim clName As New DataColumn("Name")
Dim clSurname As New DataColumn("Surname")
dtStudents.Columns.Add(clId)
dtStudents.Columns.Add(clName)
dtStudents.Columns.Add(clSurname)
For i As Integer = 0 To 20
Dim mRow As DataRow = dtStudents.NewRow
mRow.Item("Id") = i * 2
mRow.Item("Name") = "Name" & i + 1
mRow.Item("Surname") = "Surname" & i + 1
dtStudents.Rows.Add(mRow)
Next
End Sub
Private Sub lstStudents_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstStudents.SelectedValueChanged
ListItemClicked(Me.lstStudents.SelectedValue)
End Sub
Private Sub ListItemClicked(ByVal StudentId As Integer)
'Do what ever you want to do here
' LIKE: Dim cmd As New SqlClient.SqlCommand("Select * from NOTES where studentId = " & StudentId)
'bla
'bla
'bla
'You can even query the datatable.. So simple :
Dim Surname As String
'Select method of the datatable returs a row() collection, in this case it will return only one row, however you need to specify the index which is (0)
Dim row As DataRow = Me.dtStudents.Select("Id =" & StudentId)(0)
Surname = row.Item("Surname").ToString
MsgBox(Surname)
End Sub
End Class
I hope it helps, I spent 15 minutews on it :))