Bonjour,

Voici un code d'une forme, l'idée sera que lorsque on modifie la colonne quantité de mon datagridView, sa fasse automatiquement un update dans la base de donnée:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Voici donc ma form frmEditRecette:
 
using System;
 
using System.Collections.Generic;
 
using System.ComponentModel;
 
using System.Data;
 
using System.Drawing;
 
using System.Text;
 
using System.Windows.Forms;
 
using ComponentFactory.Krypton.Toolkit;
 
namespace TheProject
 
{
 
public partial class frmEditRecette : ComponentFactory.Krypton.Toolkit.KryptonForm
 
{
 
private int _idRecette;
 
public int IdRecette
 
{
 
get { return _idRecette; }
 
set { _idRecette = value; }
 
}
 
private int _idProduct;
 
public int IdProduct
 
{
 
get { return _idProduct; }
 
set { _idProduct = value; }
 
}
 
private void dataBaseReload()
 
{
 
dataBase mydatabase = new dataBase("localhost", "theproject", "root");
 
bool connectionStatus = mydatabase.SelectRecette("SELECT xdb_element.id, xdb_element.name, xdb_element.price, xdb_category.name category, xdb_recette.name recette, xdb_recettecomposant.qt, xdb_recettecomposant.id idrecettecomposant FROM xdb_element INNER JOIN xdb_recettecomposant ON xdb_element.id = xdb_recettecomposant.element INNER JOIN xdb_recette ON xdb_recettecomposant.recette = xdb_recette.id INNER JOIN xdb_category ON xdb_element.category = xdb_category.id WHERE recette=" + _idRecette + ";");
 
 
DataTable dt = this.kryptonDataGridView1.DataSource as DataTable;
 
DataTable updatedData = dt.GetChanges(DataRowState.Modified);
 
 
kryptonDataGridView1.DataSource = mydatabase.myDataTable;
 
//kryptonDataGridView1.Columns["id"].Visible = false;
 
kryptonDataGridView1.Columns["name"].HeaderText = "Nom";
 
kryptonDataGridView1.Columns["category"].HeaderText = "Catégorie";
 
kryptonDataGridView1.Columns["price"].HeaderText = "Prix/Kg";
 
kryptonDataGridView1.Columns["qt"].HeaderText = "Quantité";
 
kryptonDataGridView1.Columns["id"].Visible = true;
 
kryptonDataGridView1.Columns["idrecettecomposant"].Visible = false;
 
 
//kryptonDataGridView1.Columns["recette"].Visible = false;
 
// pour chaque ligne mise à jour on fait un UPDATE
 
bool successAll = true;
 
foreach (DataRow dr in updatedData.Rows)
 
{
 
bool updated = mydatabase.updateAll("UPDATE xdb_recetteComposant SET qt=" + dr["Quantity"].ToString() + " WHERE recette= " + dr["RecetteId"].ToString() + ";");
 
if (updated)
 
{
 
// l'update à réussi, on met à jour le statut de la ligne
 
dr.AcceptChanges();
 
}
 
successAll = successAll && updated;
 
}
 
// message utilisateur en fonction de si toutes les mises à jour ont réussi ou non
 
if (successAll)
 
{
 
MessageBox.Show("La mise à jour a été effectuée", "Mise à jour des quantités", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
}
 
else
 
{
 
MessageBox.Show("La mise à jour n'a pas pu être effectuée", "Mise à jour des quantités", MessageBoxButtons.OK, MessageBoxIcon.Warning);
 
}
 
kryptonDataGridView1.AutoGenerateColumns = true;
 
kryptonDataGridView1.Refresh();
 
DataTable table = (DataTable)kryptonDataGridView1.DataSource;
 
double totalPrice = 0;
 
foreach (DataRow row in table.Rows)
 
{
 
totalPrice += Double.Parse(row["price"].ToString());
 
totalPrice += Double.Parse(row["qt"].ToString());
 
}
 
toolStripStatusLabel1.Text = "Prix total: " + totalPrice.ToString() + "CHF";
 
bool insertPrice = mydatabase.updateAll("UPDATE xdb_recette SET price='" + totalPrice.ToString() + "' WHERE id=" + IdRecette + "");
 
}
 
public frmEditRecette()
 
{
 
InitializeComponent();
 
// bind des données
 
DataTable dt = new DataTable();
 
dt.Columns.Add("RecetteId", typeof(int));
 
dt.Columns.Add("Composant", typeof(string));
 
dt.Columns.Add("Quantity", typeof(int));
 
dt.Rows.Add(1, "Comp1", 2);
 
dt.Rows.Add(1, "Comp2", 1);
 
dt.Rows.Add(1, "Comp3", 5);
 
dt.AcceptChanges();
 
this.kryptonDataGridView1.DataSource = dt;
 
}
 
private void toolStripButton1_Click(object sender, EventArgs e)
 
{
 
frmAddTheToRecette _frmAddTheToRecette = new frmAddTheToRecette();
 
_frmAddTheToRecette.IdRecette = this.IdRecette;
 
_frmAddTheToRecette.ShowDialog();
 
dataBaseReload();
 
}
 
private void toolStripButton2_Click(object sender, EventArgs e)
 
{
 
frmAddAromToRecette _frmAddAromToRecette = new frmAddAromToRecette();
 
_frmAddAromToRecette.IdRecette = this.IdRecette;
 
_frmAddAromToRecette.ShowDialog();
 
dataBaseReload();
 
}
 
private void toolStripButton3_Click(object sender, EventArgs e)
 
{
 
frmAddDecoToRecette _frmAddDecoToRecette = new frmAddDecoToRecette();
 
_frmAddDecoToRecette.IdRecette = this.IdRecette;
 
_frmAddDecoToRecette.ShowDialog();
 
dataBaseReload();
 
}
 
private void frmEditRecette_Load(object sender, EventArgs e)
 
{
 
dataBaseReload();
 
}
 
private void toolStripButton4_Click(object sender, EventArgs e)
 
{
 
dataBase mydatabase = new dataBase("localhost", "theproject", "root");
 
bool insertArome = mydatabase.updateAll("DELETE FROM xdb_recetteComposant WHERE id=" + kryptonDataGridView1.CurrentRow.Cells["idrecettecomposant"].Value.ToString() + "");
 
if (insertArome)
 
{
 
MessageBox.Show("Suppression des données effectuées avec succès", "suppression", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
dataBaseReload();
 
}
 
else
 
{
 
MessageBox.Show("Suppression des données non effectuées", "suppression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
 
}
 
}
 
}
 
}



Quand j'exécute, il ne détecte pas d'erreur, mais dès que je vais dans la frmEdit, j'ai:

La référence d'objet n'est pas définie à une instance d'un objet.


sur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
foreach (DataRow dr in updatedData.Rows)
 
 et plus précisément sur updatedData.Rows

Si vous avez une solution je suis plus que preneur, le client veut le logiciel demain c'est la dernière étape pour que ce log soit finis :/
MeilleuresSalutation