remplir textbox depuis un datagrid
Bonjour,
en vb.net je voulais remplir des textbox à partir d'1 datagrid les 2 sont dans une form.mais dans deux groupbox différent.
je veux quant cliquant sur un enregistrement sur datagrid celui-ci s'affiche sur textbox .
Merci d'avance.
afficher ligne selectionnee dans un datagrid,textbox
bonjour meryjean,
voici 2 bouts de code qui font le travail demande pour un datagrid :
Code:
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
|
' Utilise directement.CurrentCellChanged pour mise à jour du TextBox
Public Class Form1
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable
Dim dtCol As DataColumn = New DataColumn
'ajoute 3 colonnes
dtCol = New DataColumn("Nom", GetType(String))
dt.Columns.Add(dtCol)
dtCol = New DataColumn("Prenom", GetType(String))
dt.Columns.Add(dtCol)
dtCol = New DataColumn("Numero", GetType(Integer))
dt.Columns.Add(dtCol)
Dim dtRow As DataRow
'1er ligne
dtRow = dt.NewRow
dtRow(0) = "Jacques"
dtRow(1) = "Leveque"
dtRow(2) = 1550
dt.Rows.Add(dtRow)
'2e ligne
dtRow = dt.NewRow
dtRow(0) = "Robert"
dtRow(1) = "Larlequin"
dtRow(2) = 1560
dt.Rows.Add(dtRow)
DataGrid1.DataSource = dt
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
Dim ligSel As Integer = DataGrid1.CurrentRowIndex
TextBox1.Text = ""
For elemCol As Integer = 0 To DataGrid1.VisibleColumnCount - 1
TextBox1.Text = TextBox1.Text & DataGrid1.Item(ligSel, elemCol) & " - "
Next
End Sub |
le 2eme ci-apres :
Code:
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
|
' Appelle sub qui met à jour un TextBox
Public Class Form2
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable
Dim dtCol As DataColumn = New DataColumn
'ajoute 3 colonnes
dtCol = New DataColumn("Nom", GetType(String))
dt.Columns.Add(dtCol)
dtCol = New DataColumn("Prenom", GetType(String))
dt.Columns.Add(dtCol)
dtCol = New DataColumn("Numero", GetType(Integer))
dt.Columns.Add(dtCol)
Dim dtRow As DataRow
'1er ligne
dtRow = dt.NewRow
dtRow(0) = "Jacques"
dtRow(1) = "Leveque"
dtRow(2) = 1550
dt.Rows.Add(dtRow)
'2e ligne
dtRow = dt.NewRow
dtRow(0) = "Robert"
dtRow(1) = "Larlequin"
dtRow(2) = 1560
dt.Rows.Add(dtRow)
DataGrid1.DataSource = dt
Call obtientIndexSelectionne(DataGrid1)
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
'on appelle sub qui renvoie toujours index select.
Call obtientIndexSelectionne(DataGrid1)
End Sub
Private Sub obtientIndexSelectionne(ByVal monGrid As DataGrid)
Dim ligSel As Integer = monGrid.CurrentRowIndex
TextBox2.Text = ""
For elemCol As Integer = 0 To monGrid.VisibleColumnCount - 1
TextBox2.Text = TextBox2.Text & monGrid.Item(ligSel, elemCol) & " - "
Next
End Sub
End Class |
a toi de choisir celui qui te convient
bon code...