select items combobox dans datagrid
Bonjour ,
je possède une colonne de type COMBOBOX dans une DATAGRID , et j'arrive à le remplir correctement à partir d'un Datareader , mais je ne sais pas comment afficher des données sur d'autres cellules du datagrid à partir du selectedvalue du combobox ..
le code pour charger le combobox :
Code:
1 2 3 4 5 6 7 8 9
| cmd.CommandText = "select designationArticle,fabricantArticle from Article"
Dim Dgvc As New DataGridViewComboBoxCell
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
Dgvc.Items.Add(dr(0) & " " & dr(1))
End While
DataGridView1.Item(1, 0) = Dgvc
End If |
le code pour afficher les valeurs suite à une selection d'un item du combobox :
Code:
1 2 3 4 5 6 7 8 9 10 11
| Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 Then
Dim comboBox As ComboBox = CType(e.Control, ComboBox)
If (comboBox IsNot Nothing) Then
'Remove an existing event-handler
RemoveHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
'Add the event handler.
AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
End If
End If
End Sub |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim comboBox As ComboBox = CType(sender, ComboBox)
Dim dr As SqlDataReader
Dim cmd As New SqlCommand
cn.Open()
cmd.Connection = cn
'Display selected value
MsgBox("ProgramID: " & comboBox.SelectedItem.ToString)
cmd.CommandText = "select idArticle,prix1 from ARTICLE where designationArticle like '" & comboBox.SelectedItem.ToString & "%'"
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
DataGridView1.CurrentRow.Cells(0).Value = dr(0)
DataGridView1.CurrentRow.Cells(3).Value = dr(1)
End While
End If
dr.Close()
cn.Close()
End Sub |
Merci