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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
Public Class Form1
Private salaries As DataTable
Private produits As DataTable
Private affaires As DataTable
Private ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
salaries = TableSalaries()
produits = TableProduits()
affaires = TableAffaires()
ds = New DataSet("DS Salaries")
ds.Tables.Add(salaries)
'BindingSource1.DataSource = ds
'BindingSource1.DataMember = ds.Tables("Salarie").TableName
dgv.DataSource = salaries
txtIdSalarie.DataBindings.Add("Text", salaries, "Id", True, DataSourceUpdateMode.OnPropertyChanged)
txtNomSalarie.DataBindings.Add("Text", salaries, "Nom", True, DataSourceUpdateMode.OnPropertyChanged)
txtQuantiteSalarie.DataBindings.Add("Text", salaries, "Quantite", True, DataSourceUpdateMode.OnPropertyChanged)
'en lecture seule car colonne est calculée .voir ci-apres definition table Salaries
txtTotalVente.ReadOnly = True
txtTotalVente.DataBindings.Add("Text", salaries, "TotalVente", True, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Private Function TableSalaries() As DataTable
Dim dt As New DataTable("Salarie")
dt.Columns.Add(
New DataColumn With {.Caption = "Id", .DataType = GetType(Integer), .ColumnName = "Id"})
dt.Columns.Add(
New DataColumn With {.Caption = "Nom", .DataType = GetType(String), .ColumnName = "Nom"})
dt.Columns.Add(
New DataColumn With {.Caption = "Affaire", .DataType = GetType(String), .ColumnName = "Affaire"})
dt.Columns.Add(
New DataColumn With {.Caption = "Quantite", .DataType = GetType(Integer), .ColumnName = "Quantite"})
dt.Columns.Add(
New DataColumn With {.Caption = "Produit", .DataType = GetType(Decimal), .ColumnName = "PrixProduit"})
'cette colonne sera calculée: Quantite x PrixProduit
dt.Columns.Add(
New DataColumn With {.Caption = "Total", .DataType = GetType(Decimal), .ColumnName = "TotalVente"})
Return dt
End Function
Private Function TableProduits() As DataTable
Dim dt As New DataTable("Produit")
dt.Columns.Add(
New DataColumn With {.Caption = "Id", .DataType = GetType(Integer), .ColumnName = "IdProduit"})
dt.Columns.Add(
New DataColumn With {.Caption = "Nom", .DataType = GetType(String), .ColumnName = "NomProduit"})
dt.Columns.Add(
New DataColumn With {.Caption = "Prix", .DataType = GetType(Decimal), .ColumnName = "PrixProduit"})
'fill table
Dim dr As DataRow = dt.NewRow
dt.Rows.Add("101", "produit1", 100.25)
dt.Rows.Add("102", "produit2", 125.5)
dt.Rows.Add("103", "produit3", 200.55)
Return dt
End Function
Private Function TableAffaires() As DataTable
Dim dt As New DataTable("Affaire")
dt.Columns.Add(
New DataColumn With {.Caption = "Id", .DataType = GetType(Integer), .ColumnName = "IdAffaire"})
dt.Columns.Add(
New DataColumn With {.Caption = "Affaire", .DataType = GetType(String), .ColumnName = "NomAffaire"})
'fill table
dt.Rows.Add("5001", "Affaire1")
dt.Rows.Add("5002", "Affaire2")
dt.Rows.Add("5003", "Affaire3")
dt.Rows.Add("5004", "Affaire4")
dt.Rows.Add("5005", "Affaire5")
Return dt
End Function
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
End Sub
Private Sub dgv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellClick
If (e.ColumnIndex > -1) Then
'Bind grid cell with combobox and than bind combobox with datasource.
Dim cboAffaires As New DataGridViewComboBoxCell()
Dim cboProduits As New DataGridViewComboBoxCell()
' Check the column cell, in which it click.
If (dgv.Columns(e.ColumnIndex).Name.Contains("Affaire")) Then
' On click of datagridview cell, attched combobox with this click cell of datagridview
dgv(e.ColumnIndex, e.RowIndex) = cboAffaires
cboAffaires.DataSource = TableAffaires() ' Bind combobox with datasource.
cboAffaires.DisplayMember = "NomAffaire"
cboAffaires.ValueMember = "NomAffaire"
End If
If (dgv.Columns(e.ColumnIndex).Name.Contains("Produit")) Then
dgv(e.ColumnIndex, e.RowIndex) = cboProduits
cboProduits.DataSource = TableProduits()
cboProduits.DisplayMember = "NomProduit"
cboProduits.ValueMember = "PrixProduit"
End If
'maj de la colonne TotalVente (calcul)
If (dgv.Columns(e.ColumnIndex).Name.Contains("TotalVente")) Then
dgv.Rows(e.RowIndex).Cells("TotalVente").Value =
CType(dgv.Rows(e.RowIndex).Cells("Quantite").Value, Decimal) * CType(dgv.Rows(e.RowIndex).Cells("PrixProduit").Value, Decimal)
End If
End If
End Sub
End Class |