以前にも数回同じようなソフトを作っているが、今回は
表を並べて表示するタイプ。
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; using System.IO; namespace 預かり在庫管理ver2 { public partial class Form1 : Form { public void dgv1_setup() { string[] header = new string[] { "ID", "登録日", "伝票番号", "得意先", "仕入先", "品目名", "PO", "INV", "数量", "備考" }; foreach (string h in header) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = h; dataGridView1.Columns.Add(col); } for (int i = 1; i <= 2; i++) { DataGridViewButtonColumn col_btn = new DataGridViewButtonColumn(); col_btn.HeaderText = " "; dataGridView1.Columns.Add(col_btn); } dataGridView1.Columns[0].Visible = false; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.ReadOnly = true; dataGridView1.AllowUserToAddRows = false; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.RowTemplate.Height = 28; dataGridView1.Font = new Font("Meiryo UI", 9); dataGridView1.MultiSelect = false; dataGridView1.CellClick += new DataGridViewCellEventHandler(cell_click); } public void dgv2_setup() { string[] header = new string[] { "ID", "出庫日", "出庫数" }; foreach (string h in header) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.Name = h; col.HeaderText = h; dataGridView2.Columns.Add(col); } DataGridViewButtonColumn col_btn = new DataGridViewButtonColumn(); col_btn.HeaderText = " "; dataGridView2.Columns.Add(col_btn); dataGridView2.Columns[0].Visible = false; dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView2.ReadOnly = true; dataGridView2.AllowUserToAddRows = false; dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView2.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView2.RowTemplate.Height = 28; dataGridView2.Font = new Font("Meiryo UI", 9); dataGridView2.MultiSelect = false; dataGridView2.CellClick += new DataGridViewCellEventHandler(sub_cell_click); } public Form1() { InitializeComponent(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd1 = new OleDbCommand("select distinct customer_name from orders;", con); using (OleDbDataReader dr = cmd1.ExecuteReader()) { while (dr.Read()) { comboBox1.Items.Add(dr["customer_name"].ToString()); } } OleDbCommand cmd2 = new OleDbCommand("select distinct supplier_name from orders;", con); using (OleDbDataReader dr = cmd2.ExecuteReader()) { while (dr.Read()) { comboBox2.Items.Add(dr["supplier_name"].ToString()); } } OleDbCommand cmd3 = new OleDbCommand("select distinct item_name from orders;", con); using (OleDbDataReader dr = cmd3.ExecuteReader()) { while (dr.Read()) { comboBox3.Items.Add(dr["item_name"].ToString()); } } } catch (OleDbException e) { foreach (Control c in this.Controls) { c.Enabled = false; } MessageBox.Show(e.Message); return; } finally { con.Close(); } } textBox1.TextChanged += new EventHandler((object sender, EventArgs e) => { button1.Text = textBox1.Text == "" ? "登録" : "更新"; }); dgv1_setup(); dgv2_setup(); dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); } private string rp(string s) { s = s.Replace("\'", "’"); s = s.Replace("\"", "”"); return s; } private void sub_cell_click(object sender, DataGridViewCellEventArgs e) { int btn_ci = 3; int ri = e.RowIndex; int ci = e.ColumnIndex; if (ri == -1 || ci == -1) return; string id = dataGridView2.Rows[ri].Cells[0].Value.ToString(); if (ci == btn_ci) { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from shipments where shipments_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } sub_dgv_load(textBox1.Text); } } private void cell_click(object sender, DataGridViewCellEventArgs e) { int btn1_ci = 10; int btn2_ci = 11; int btn3_ci = 6; int ri = e.RowIndex; int ci = e.ColumnIndex; if (ri == -1 || ci == -1) return; string id = dataGridView1.Rows[ri].Cells[0].Value.ToString(); string po = dataGridView1.Rows[ri].Cells[6].Value.ToString(); string btn_cap = dataGridView1.Rows[ri].Cells[btn1_ci].Value.ToString(); if (ci == btn1_ci) { rows_change(id, btn_cap); dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); return; } else if (ci == btn2_ci) { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from orders where orders_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } dgv_load("select * from orders where visible <> 'hide';", "完了"); form_clear(); return; } else if (ci == btn3_ci) { open_Folder(po); return; } textBox1.Text = dataGridView1.Rows[ri].Cells[0].Value.ToString(); textBox8.Text = dataGridView1.Rows[ri].Cells[1].Value.ToString(); textBox2.Text = dataGridView1.Rows[ri].Cells[2].Value.ToString(); comboBox1.Text = dataGridView1.Rows[ri].Cells[3].Value.ToString(); comboBox2.Text = dataGridView1.Rows[ri].Cells[4].Value.ToString(); comboBox3.Text = dataGridView1.Rows[ri].Cells[5].Value.ToString(); textBox5.Text = dataGridView1.Rows[ri].Cells[6].Value.ToString(); textBox6.Text = dataGridView1.Rows[ri].Cells[7].Value.ToString(); textBox9.Text = dataGridView1.Rows[ri].Cells[8].Value.ToString(); textBox7.Text = dataGridView1.Rows[ri].Cells[9].Value.ToString(); sub_dgv_load(id); } private void sub_dgv_load(string id) { dataGridView2.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("select * from shipments where orders_id = " + id + ";", con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { dataGridView2.Rows.Add( dr["shipments_id"].ToString(), DateTime.Parse(dr["ship_date"].ToString()).ToString("yyyy/MM/dd"), dr["quantity"].ToString(), "削除"); } } } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } int stock_count = 0; for (int r = 0; r < dataGridView2.Rows.Count; r++) { stock_count += int.Parse(dataGridView2.Rows[r].Cells[2].Value.ToString()); } textBox12.Text = stock_count.ToString(); } private void rows_change(string id,string btn_cap) { string tmp = (btn_cap == "完了") ? "hide" : "show"; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("update orders set visible = '" + tmp + "' where orders_id = " + id + ";", con); cmd.ExecuteNonQuery(); } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } } private void form_clear() { textBox1.Text = ""; textBox8.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox2.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox9.Text = ""; textBox7.Text = ""; textBox14.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox12.Text = ""; textBox10.Text = ""; dataGridView2.Rows.Clear(); } private bool form_validate() { if (textBox8.Text == "" | textBox2.Text == "" | comboBox1.Text == "" | comboBox2.Text == "" | comboBox3.Text == "" | textBox5.Text == "" | textBox6.Text == "" | textBox9.Text == "") { MessageBox.Show("入力が不足しています。"); return false; } try { DateTime.Parse(textBox8.Text); } catch { MessageBox.Show("日付が正しくありません。"); return false; } try { int.Parse(textBox9.Text); } catch { MessageBox.Show("数量が正しくありません。"); return false; } textBox2.Text = rp(textBox2.Text); comboBox1.Text = rp(comboBox1.Text); comboBox2.Text = rp(comboBox2.Text); comboBox3.Text = rp(comboBox3.Text); textBox5.Text = rp(textBox5.Text); textBox6.Text = rp(textBox6.Text); textBox7.Text = rp(textBox7.Text); return true; } private bool change_tbl() { string query; if (button1.Text == "登録") { query = "insert into orders (register_date,order_no,customer_name,supplier_name,item_name,po_no,inv_no,quantity,notes,visible) values (" + "'" + textBox8.Text + "'," + "'" + textBox2.Text + "'," + "'" + comboBox1.Text + "'," + "'" + comboBox2.Text + "'," + "'" + comboBox3.Text + "'," + "'" + textBox5.Text + "'," + "'" + textBox6.Text + "'," + "" + textBox9.Text + "," + "'" + textBox7.Text + "','show');"; } else { query = "update orders set " + "register_date='" + textBox8.Text + "'," + "order_no='" + textBox2.Text + "'," + "customer_name='" + comboBox1.Text + "'," + "supplier_name='" + comboBox2.Text + "'," + "item_name='" + comboBox3.Text + "'," + "po_no='" + textBox5.Text + "'," + "inv_no='" + textBox6.Text + "'," + "quantity=" + textBox9.Text + "," + "notes='" + textBox7.Text + "'" + "where orders_id = " + textBox1.Text + ";"; } using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query, con); cmd.ExecuteNonQuery(); } catch (OleDbException e) { MessageBox.Show(e.Message); return false; } finally { con.Close(); } } return true; } private void dgv_load(string query,string btn_cap) { dataGridView1.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query,con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { dataGridView1.Rows.Add( dr["orders_id"].ToString(), DateTime.Parse( dr["register_date"].ToString()).ToString("yyyy/MM/dd"), dr["order_no"].ToString(), dr["customer_name"].ToString(), dr["supplier_name"].ToString(), dr["item_name"].ToString(), dr["po_no"].ToString(), dr["inv_no"].ToString(), dr["quantity"].ToString(), dr["notes"].ToString(), btn_cap,"削除"); } } } catch (OleDbException e) { MessageBox.Show(e.Message); } finally { con.Close(); } } if (btn_cap == "完了") { button6.Enabled = false; button5.Enabled = true; } if (btn_cap == "戻入") { button6.Enabled = true; button5.Enabled = false; } } private void button1_Click(object sender, EventArgs e) { if (!form_validate()) return; if (!change_tbl()) return; if (button1.Text == "登録") { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (button1.Text == "更新" && button5.Enabled == true) { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (button1.Text == "更新" && button5.Enabled == false) { dgv_load("select * from orders where visible = 'hide';", "戻入"); } form_clear(); MessageBox.Show("完了しました。"); } private void button3_Click(object sender, EventArgs e) { form_clear(); } private bool sub_form_validate() { if (textBox14.Text == "" | textBox13.Text == "") { MessageBox.Show("入力が不足しています。"); return false; } try { DateTime.Parse(textBox14.Text); } catch { MessageBox.Show("日付が正しくありません。"); return false; } try { int.Parse(textBox13.Text); } catch { MessageBox.Show("数量が正しくありません。"); return false; } return true; } private void button4_Click(object sender, EventArgs e) { if (!form_validate()) return; if (!sub_form_validate()) return; if (textBox1.Text == "") return; string query = "insert into shipments (orders_id,ship_date,quantity) values " + "(" + textBox1.Text + ",'" + textBox14.Text + "'," + textBox13.Text + ");"; using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0; data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(query, con); cmd.ExecuteNonQuery(); } catch (OleDbException ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } sub_dgv_load(textBox1.Text); textBox14.Text = DateTime.Now.ToString("yyyy/MM/dd"); textBox13.Text = ""; } private void button6_Click(object sender, EventArgs e) { dgv_load("select * from orders where visible <> 'hide';","完了"); form_clear(); } private void button5_Click_1(object sender, EventArgs e) { dgv_load("select * from orders where visible = 'hide';","戻入"); form_clear(); } private void button2_Click(object sender, EventArgs e) { string q = ""; textBox10.Text = rp(textBox10.Text); if (textBox10.Text == "" && button5.Enabled == true) { dgv_load("select * from orders where visible <> 'hide';", "完了"); } else if (textBox10.Text == "" && button5.Enabled == false) { dgv_load("select * from orders where visible = 'hide';", "戻入"); } else if (textBox10.Text != "" && button5.Enabled == true) { q = "select * from orders where visible <> 'hide' and " + "(order_no like '%" + textBox10.Text + "%' or " + "customer_name like '%" + textBox10.Text + "%' or " + "supplier_name like '%" + textBox10.Text + "%' or " + "item_name like '%" + textBox10.Text + "%' or " + "po_no like '%" + textBox10.Text + "%' or " + "inv_no like '%" + textBox10.Text + "%' or " + "notes like '%" + textBox10.Text + "%')"; dgv_load(q, "完了"); } else if (textBox10.Text != "" && button5.Enabled == false) { q = "select * from orders where visible = 'hide' and " + "(order_no like '%" + textBox10.Text + "%' or " + "customer_name like '%" + textBox10.Text + "%' or " + "supplier_name like '%" + textBox10.Text + "%' or " + "item_name like '%" + textBox10.Text + "%' or " + "po_no like '%" + textBox10.Text + "%' or " + "inv_no like '%" + textBox10.Text + "%' or " + "notes like '%" + textBox10.Text + "%')"; dgv_load(q, "戻入"); } } void open_Folder(string po) { string path = ""; try { using (StreamReader sr = new StreamReader(Application.StartupPath + @"\setting.txt", Encoding.GetEncoding("shift_jis"))) { path = sr.ReadLine() + @"\" + po; if (Directory.Exists(path)) { System.Diagnostics.Process.Start(path); } else { DialogResult yn = MessageBox.Show("フォルダを作成します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; Directory.CreateDirectory(path); System.Diagnostics.Process.Start(path); } } } catch (Exception e) { MessageBox.Show(e.Message); } } } } |