1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| Imports System.Data.OleDb
Public Class Form1
Private Connection As New OleDbConnection
Private ObjetDataAdapter As OleDbDataAdapter
Private ds As New DataSet
Private Sub Initdbase()
Dim base As String = "provider=microsoft.jet.oledb.4.0;data source= C:\Users\Milardi\Documents\Contacts.accdb"
Try
Connection.ConnectionString = base
Connection.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub GetALLdata()
Dim sql As String
Dim ObjetCommand As New OleDbCommand
Dim da As OleDb.OleDbDataAdapter
sql = "SELECT * FROM CONTACTS "
Try
ObjetCommand = New OleDbCommand(sql)
ObjetCommand.Connection() = Connection
da = New OleDbDataAdapter(ObjetCommand)
Connection.Close()
da.Fill(ds, "LES CONTACTS")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'ds contient une table nommée "LES CONTACTS"
' tu peux lire cette table par ex mettre tous les noms dans un listbox
' si NOM est le champ 1 de la table "LES CONTACTS
Private Sub FillListBox()
GetALLdata()
ListBox1.Items.Clear()
For Each row As DataRow In ds.Tables("LES CONTACTS").Rows
ListBox1.Items.Add(row.Item(1).ToString)
Next
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FillListBox()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'afficher dans un dgv les données
Dim line As Integer
Dim row As DataRow
line = DirectCast(sender, ListBox).SelectedIndex
row = ds.Tables("CONTACT").Rows(line)
'row contient tous les données du nom choisi
'il suffit de lire row dans un composant adapté comme un datagridview
DataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken
DataGridView1.Font = New Font("arial", 14, FontStyle.Bold, GraphicsUnit.Pixel)
DataGridView1.AutoResizeColumns()
DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
DataGridView1.EnableHeadersVisualStyles = False
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.PowderBlue
DataGridView1.Columns.Clear()
DataGridView1.Rows.Clear()
For col = 0 To ds.Tables("LES CONTACTS").Columns.Count - 1
DataGridView1.Columns.Add(row(col).ToString, " ")
DataGridView1.Rows.Item(0).Cells(col).Value = row(col).ToString
Next
End Sub
End Class |
Partager