private void Update_Click object sender EventArgs Query switch comboBo

  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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
private void Update_Click(object sender, EventArgs e)
{ Query = "";
switch (comboBox1.SelectedIndex)
{
case 0:
{
string name = "NULL";
string author = "NULL";
string name_au = "NULL";
string genre ="NULL";
string year = "NULL";
string lim = "NULL";
string film ="NULL";
string pic= "NULL";
string publish="NULL";
if (dataGridView1[0, row].Value.ToString() != "") name = "'" + dataGridView1[0, row].Value.ToString() + "'";
if (dataGridView1[1, row].Value.ToString() != "") author = "'" + dataGridView1[1, row].Value.ToString() + "'";
if (dataGridView1[2, row].Value.ToString() != "") name_au = "'" + dataGridView1[2, row].Value.ToString() + "'";
if (dataGridView1[3, row].Value.ToString() != "") publish = "'" + dataGridView1[3, row].Value.ToString() + "'";
if (dataGridView1[4, row].Value.ToString() != "") year = dataGridView1[4, row].Value.ToString();
year = new string(year.Where(ch => char.IsDigit(ch)).ToArray());
if (dataGridView1[5, row].Value.ToString() != "") genre = "'" + dataGridView1[5, row].Value.ToString() + "'";
if (dataGridView1[6, row].Value.ToString() != "") pic = "'" + dataGridView1[6, row].Value.ToString() + "'";
if (dataGridView1[7, row].Value.ToString() != "") film = "'" + dataGridView1[7, row].Value.ToString() + "'";
if (dataGridView1[8, row].Value.ToString() != "") lim = dataGridView1[8, row].Value.ToString();
if (name == "NULL") MessageBox.Show("Нельзя ввести данные о книге без названия!");
else
{Query = "update book set name=" + name + ",id_author=(select author.id_author from author where author.surname=" + author + " && author.name=" + name_au + "), id_genre=(select genre.id_genre from genre where genre.name=" + genre + "),year=" + year + ",limit_age=" + lim + ",film=" + film + ",picture=" + pic + ",id_publish=(select publish.id_publish from publish where publish.name=" + publish + ") where name=" + n_old + " ;";
Query = Query + "update book set id_author = 11 where id_author is NULL; update book set id_publish = 8 where id_publish is NULL; update book set id_genre = 15 where id_genre is NULL; ";
currentDate ="\t"+ DateTime.Now.ToString();
System.IO.File.AppendAllText(Directory.GetCurrentDirectory() + @"\files\journal.txt",Environment.NewLine+Environment.NewLine+ currentDate + " " + Query);
Query = Query + "select book.name as Название, author.surname as Фамилия_автора, author. name as Имя_автора, publish.name as Издательство, year as год, genre.name as жанр, picture as Иллюстрации, film as Экранизация, limit_age as Возр_огр from book, genre,author, publish where book.id_author=author.id_author && book.id_genre=genre.id_genre && book.id_publish=publish.id_publish ;";
}break;
}
case 1:
{
string name="NULL";
string surname="NULL";
string second_name="NULL";
string year_b="NULL";
string year_d="NULL";
string nat="NULL";
string act="NULL";
if (dataGridView1[0, row].Value.ToString()!="") name = "'" + dataGridView1[0, row].Value.ToString() + "'";
if (dataGridView1[1, row].Value.ToString() != "") surname = "'" + dataGridView1[1, row].Value.ToString() + "'";
if (dataGridView1[2, row].Value.ToString() != "") second_name = "'" + dataGridView1[2, row].Value.ToString() + "'";
if (dataGridView1[3, row].Value.ToString() != "") year_b = dataGridView1[3, row].Value.ToString();
year_b = new string(year_b.Where(ch => char.IsDigit(ch)).ToArray());
if (dataGridView1[4, row].Value.ToString() != "") year_d = dataGridView1[4, row].Value.ToString();
year_d = new string(year_d.Where(ch => char.IsDigit(ch)).ToArray());
if (dataGridView1[5, row].Value.ToString() != "") nat = "'" + dataGridView1[5, row].Value.ToString() + "'";
if (dataGridView1[6, row].Value.ToString() != "") act = "'" + dataGridView1[6, row].Value.ToString() + "'";
if (name == "NULL" && surname=="NULL") MessageBox.Show("Нельзя ввести данные об авторе без имени и фамилии!");
else
{
Query = "update author set name=" + name + ",surname=" + surname + ",second_name=" + second_name + ",year_b=" + year_b + ",year_d=" + year_d + ",nationality=" + nat + ",id_act=(select act.id_act from act where act.name=" + act + ") where name=" + n_old + " && surname=" + author_old + ";";
Query = Query + "update author set id_act = 10 where id_act is NULL;";
currentDate ="\t"+ DateTime.Now.ToString();
System.IO.File.AppendAllText(Directory.GetCurrentDirectory() + @"\files\journal.txt", Environment.NewLine+ currentDate + " " + Query +Environment.NewLine);
Query = Query + "select author.name as Имя, surname as Фамилия, second_name as отчество,year_b as Год_рождения, year_d as Год_смерти , nationality as Страна, act.name as Род_деятельности from author, act where author.id_act = act.id_act ;";
} break;
}
case 2:
{
string name="NULL";
string desc = "NULL";
string cent = "NULL";
if (dataGridView1[0, row].Value.ToString() != "") name = "'" + dataGridView1[0, row].Value.ToString() + "'";
if (dataGridView1[1, row].Value.ToString() != "") cent = "'" + dataGridView1[1, row].Value.ToString() + "'";
cent = new string(cent.Where(ch => char.IsDigit(ch)).ToArray());
if (dataGridView1[2, row].Value.ToString() != "") desc = "'" + dataGridView1[2, row].Value.ToString() + "'";
if (name == "NULL") MessageBox.Show("Нельзя ввести данные о жанре без названия!");
else
{
Query = "update genre set name=" + name + ",century_bd=" + cent + ",description=" + desc + "where name=" + n_old + ";";
currentDate ="\t"+ DateTime.Now.ToString();
System.IO.File.AppendAllText(Directory.GetCurrentDirectory() + @"\files\journal.txt", Environment.NewLine+Environment.NewLine + currentDate + " " + Query);
Query = Query + "select name as Название, century_bd as Век_возникновения, description as Описание from genre;";
} break;
}
case 3:
{ string name="NULL";
string desc = "NULL";
if (dataGridView1[0, row].Value.ToString() != "") name = "'" + dataGridView1[0, row].Value.ToString() + "'";
if (dataGridView1[1, row].Value.ToString() != "") desc = "'" + dataGridView1[1, row].Value.ToString() + "'";
if (name == "NULL") MessageBox.Show("Нельзя ввести данные о деятельности без названия!");
else
{
Query = "update act set name=" + name + ",description=" + desc + "where name=" + n_old + ";";
currentDate ="\t"+ DateTime.Now.ToString();
System.IO.File.AppendAllText(Directory.GetCurrentDirectory() + @"\files\journal.txt", Environment.NewLine+Environment.NewLine + currentDate + " " + Query);
Query = Query + "select name as Название, description as Описание from act;";
}
break;
}
case 4:
{
string name="NULL";
string city = "NULL";
string address = "NULL";
string phone = "NULL";
string home_page = "NULL";
string e_mail = "NULL";
if (dataGridView1[0, row].Value.ToString() != "") name = "'" + dataGridView1[0, row].Value.ToString() + "'";
if (dataGridView1[1, row].Value.ToString() != "") city = "'" + dataGridView1[1, row].Value.ToString() + "'";
if (dataGridView1[2, row].Value.ToString() != "") address = "'" + dataGridView1[2, row].Value.ToString() + "'";
if (dataGridView1[3, row].Value.ToString() != "") phone = "'" + dataGridView1[3, row].Value.ToString() + "'";
if (dataGridView1[4, row].Value.ToString() != "") home_page = "'" + dataGridView1[4, row].Value.ToString() + "'";
if (dataGridView1[5, row].Value.ToString() != "") e_mail = "'" + dataGridView1[5, row].Value.ToString() + "'";
if (name == "NULL") MessageBox.Show("Нельзя ввести данные об издательстве без названия!");
else
{
Query = "update publish set name=" + name + ",city=" + city + ",address=" + address + ",phone=" + phone + ",home_page = " + home_page + ",e_mail=" + e_mail + " where name = " + n_old + ";";
Query = Query + "update book set id_publish = 8 where id_publish is NULL; ";
currentDate ="\t"+ DateTime.Now.ToString();
System.IO.File.AppendAllText(Directory.GetCurrentDirectory() + @"\files\journal.txt", Environment.NewLine+ currentDate + " " + Query +Environment.NewLine);
Query = Query + "select name as Название, city as Город,address as Адрес, phone as Телефон, home_page as Сайт , e_mail as Почта from publish ;";
}
break;
}
default: { Query = ""; break; }
}
MyCom = new MySqlCommand(Query, MyCon);
MyAdapter = new MySqlDataAdapter();
MyAdapter.SelectCommand = MyCom;
dTable = new DataTable();
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
dataGridView1.Controls.Clear();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
currentRow = e.RowIndex;
if (e.RowIndex != -1 && e.ColumnIndex != -1 && !dataGridView1.ReadOnly)
{
cellClick = true;
string col = dt.Columns[e.ColumnIndex].ColumnName;
switch (col)
{
case "Фамилия_автора":
{
ComboBox cmb = new ComboBox();
cmb.DataSource = querry.getTable("select surname as Фамилия from author;");
cmb.DisplayMember = "Фамилия";
cmb.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmb.Name = "cmb_surAu";
Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cmb.Location = new Point(rect.X, rect.Y);
cmb.Size = new Size(rect.Width, rect.Height);
dataGridView1.Controls.Add(cmb);
break;
}
case "Имя_автора":
{
ComboBox cmb1 = new ComboBox();
cmb1.DataSource = querry.getTable("select name as Имя from author;");
cmb1.DisplayMember = "Имя";
cmb1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmb1.Name = "cmb_nameAu";
Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cmb1.Location = new Point(rect.X, rect.Y);
cmb1.Size = new Size(rect.Width, rect.Height);
dataGridView1.Controls.Add(cmb1);
cmb1.SelectedIndex = ((int)cmb1.Items.IndexOf(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
break;
}
case "жанр":
{
ComboBox cmb2 = new ComboBox();
cmb2.DataSource = querry.getTable("select name as Название from genre;");
cmb2.DisplayMember = "Название";
cmb2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmb2.Name = "cmb_genre";
Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cmb2.Location = new Point(rect.X, rect.Y);
cmb2.Size = new Size(rect.Width, rect.Height);
dataGridView1.Controls.Add(cmb2);
cmb2.SelectedIndex = ((int)cmb2.Items.IndexOf(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
break;
}
case "Издательство":
{
ComboBox cmb3 = new ComboBox();
cmb3.DataSource = querry.getTable("select name as Название from publish;");
cmb3.DisplayMember = "Название";
cmb3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmb3.Name = "cmb_publish";
Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cmb3.Location = new Point(rect.X, rect.Y);
cmb3.Size = new Size(rect.Width, rect.Height);
dataGridView1.Controls.Add(cmb3);
cmb3.SelectedIndex = ((int)cmb3.Items.IndexOf(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
break;
}
case "Род_деятельности":
{
ComboBox cmb4 = new ComboBox();
cmb4.DataSource = querry.getTable("select name as Название from act;");
cmb4.DisplayMember = "Название";
cmb4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmb4.Name = "cmb_act";
Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cmb4.Location = new Point(rect.X, rect.Y);
cmb4.Size = new Size(rect.Width, rect.Height);
dataGridView1.Controls.Add(cmb4);
cmb4.SelectedIndex = (int)(cmb4.Items.IndexOf(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
break;
}
}
}
cellClick = true;
}
public void dgv_CellCancel(object sender, DataGridViewCellEventArgs e) // определила событие выхода из ячейки(Интер нажала, к примеру). Тут в бд записывать будем
{
if (cellClick)
{
try
{
string col = dt.Columns[e.ColumnIndex].ColumnName;
string data = "";
switch (col)
{
case "Фамилия_автора":
{
if (dataGridView1.Controls["cmb_surAu"] != null)
{
data = ((ComboBox)dataGridView1.Controls["cmb_surAu"]).Text; // спихиваем с него значение в переменную
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data; /// Результат оставляешь в ячейке на гриде
}
break;
}
case "Имя_автора":
{
if (dataGridView1.Controls["cmb_nameAu"] != null)
{
data = ((ComboBox)dataGridView1.Controls["cmb_nameAu"]).Text; // спихиваем с него значение в переменную
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data; /// Результат оставляешь в ячейке на гриде
}
break;
}
case "жанр":
{
if (dataGridView1.Controls["cmb_genre"] != null)
{
data = ((ComboBox)dataGridView1.Controls["cmb_genre"]).Text; // спихиваем с него значение в переменную
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data; /// Результат оставляешь в ячейке на гриде
}
break;
}
case "Издательство":
{
if (dataGridView1.Controls["cmb_publish"] != null)
{
data = ((ComboBox)dataGridView1.Controls["cmb_publish"]).Text; // спихиваем с него значение в переменную
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data; /// Результат оставляешь в ячейке на гриде
}
break;
}
case "Род_деятельности":
{
string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (dataGridView1.Controls["cmb_act"] != null)
{
data = ((ComboBox)dataGridView1.Controls["cmb_act"]).Text; // спихиваем с него значение в переменную
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data; /// Результат оставляешь в ячейке на гриде
}
break;
}
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong:\n" + ex.ToString());
}
cellClick = false;
}
}