Mise à jour SQL depuis un DATAGRIDVIEW et TXTBOX
Bonjour à tous,
Je ne vois pas de solution facile et rapide pour mettre à jour ma base de données SQL2008 STD.
Condition:
J'ai une FORM qui contient 2 Datagridview (l'un sous l'autre) et correctement chargés.
Le premier (DG_Planning) contient une agrégation de données pour ne donner qu'une ligne de quantité totale à produire.
Le second (DG_detail) contient le détail de la ligne sélectionnée dans DG_Planning, soit 1 ou x lignes (on peut pas savoir).
Lors d'un click ou changement de selection dans DG_Planning, DG_Detail est actualisé.
Sur le coté, j'ai un TXTbox vide. L'utilisateur doit sélectionner une ligne de DG_Planning puis renseigner le Txtbox (alphanumérique) et enfin valider.
Le fait de valider doit ajouter la valeur de mon TXTBOX autant de fois qu'il y a de ligne dans DG_Detail (puisque ma base de données est ainsi faite).
Le problème est que je ne vois pas comment aborder cet update..
Un peu de code:
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
| Private Sub InitializeDataGridView_DG_Planning()
Try
' Remplissage du DataGridView.
With Me.DG_Planning
.AutoGenerateColumns = True
bindingPlanning.DataSource = GetData("SELECT UP,CM,NOMENCLATURE,LIBELLE,QUALITE,TAUX_RC,DIAM,[DATE] " & _
" ,MICRO,CAST(PCHARGE as FLOAT) / 10 as PCHARGE, SUM(NBC) AS NBC, Retard " & _
" FROM dbo.PDC01_Preparation_Charge " & _
" GROUP BY UP,CM,NOMENCLATURE,LIBELLE,QUALITE,TAUX_RC,DIAM,[DATE],MICRO,PCHARGE,RETARD " & _
" ORDER BY Retard DESC ")
.DataSource = bindingPlanning
.RowTemplate.Height = 30
.BorderStyle = BorderStyle.Fixed3D
End With
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "PDC Planning")
System.Threading.Thread.CurrentThread.Abort()
End Try
End Sub |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Private Sub InitializeDataGridView_DG_Detail()
Try
With Me.DG_Detail
.AutoGenerateColumns = True
bindingDetail.DataSource = GetData("SELECT [DATE] ,[MICRO],[OF],[MPN],[PCHARGE] / 10 as PCHARGE, [NBC] " & _
" FROM PDC01_Preparation_Charge WHERE [DATE] = '" & tb_date.Text & "' AND MICRO = '" & tb_Micro.Text & "' ")
.DataSource = bindingDetail
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
.BorderStyle = BorderStyle.Fixed3D
End With
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "PDC Planning")
System.Threading.Thread.CurrentThread.Abort()
End Try
END SUB |
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
| Private Shared Function GetData(ByVal sqlCommand As String) _
As DataTable
Try
Dim connectionString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" _
& "Initial Catalog= " & Base & " ;Data Source= " & DataSource & ""
Dim myConnection As SqlConnection = _
New SqlConnection(connectionString)
Dim command As New SqlCommand(sqlCommand, myConnection)
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
adapter.SelectCommand = command
Dim table As New DataTable
table.Locale = System.Globalization.CultureInfo.InvariantCulture
adapter.Fill(table)
Return table
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function |
Merci pour vos idées!