FormManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace СУБД_Швейная_фабрика
  12. {
  13. public partial class FormManager : Form
  14. {
  15. public FormManager()
  16. {
  17. InitializeComponent();
  18. }
  19. private void btnEdit_Click(object sender, EventArgs e)
  20. {
  21. FormEditUser frm = new FormEditUser();
  22. this.Hide();
  23. frm.ShowDialog();
  24. this.Show();
  25. }
  26. private void FormManager_Load(object sender, EventArgs e)
  27. {
  28. // TODO: This line of code loads data into the 'fabrikaLDADataSet.Products' table. You can move, or remove it, as needed.
  29. this.productsTableAdapter.Fill(this.fabrikaLDADataSet.Products);
  30. SqlConnection con = new SqlConnection(FormAuthorization.StrCon);
  31. con.Open();
  32. SqlCommand cmd = new SqlCommand($@"select Fam,Name,Otch from Users where IdUser = {FormAuthorization.IdUser}", con);
  33. SqlDataReader res = cmd.ExecuteReader();
  34. res.Read();
  35. lblFIO.Text = $"{res["Fam"]} {res["Name"]} {res["Otch"]}";
  36. res.Close();
  37. con.Close();
  38. }
  39. public static int Pos = -1;
  40. private void btnAdd_Click(object sender, EventArgs e)
  41. {
  42. try
  43. {
  44. if (tbxTitle.Text == "" || int.Parse(tbxLenght.Text) <= 0 || int.Parse(tbxWidth.Text) <= 0)
  45. {
  46. MessageBox.Show("Все поля должны быть заполнены и больше нуля", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47. return;
  48. }
  49. }
  50. catch
  51. {
  52. MessageBox.Show("Все поля должны быть заполнены и больше нуля", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  53. return;
  54. }
  55. SqlConnection con = new SqlConnection(FormAuthorization.StrCon);
  56. con.Open();
  57. if (btnAdd.Text == "Сохранить" && Pos != -1)
  58. {
  59. SqlCommand cmdManager = new SqlCommand($@"update Products
  60. set TitleProduct = '{tbxTitle.Text}',Lenght={tbxLenght.Text},Width={tbxWidth.Text}
  61. where IdProduct = {Pos}", con);
  62. cmdManager.ExecuteNonQuery();
  63. con.Close();
  64. MessageBox.Show("Изделие отредактировано", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  65. }
  66. else if (btnAdd.Text == "Добавить") {
  67. SqlCommand cmdManager = new SqlCommand($@"Insert into Products(TitleProduct,Lenght,Width)
  68. values('{tbxTitle.Text}',{tbxLenght.Text},{tbxWidth.Text})", con);
  69. cmdManager.ExecuteNonQuery();
  70. con.Close();
  71. MessageBox.Show("Изделие добавлено", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  72. }
  73. dgvProducts.Update();
  74. }
  75. private void btnDrop_Click(object sender, EventArgs e)
  76. {
  77. Pos = -1;
  78. btnAdd.Text = "Добавить";
  79. tbxTitle.Text = "";
  80. tbxLenght.Text = "";
  81. tbxWidth.Text = "";
  82. }
  83. private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
  84. {
  85. btnAdd.Text = "Сохранить";
  86. Pos = int.Parse(dgvProducts.CurrentRow.Cells[0].Value.ToString());
  87. tbxTitle.Text = dgvProducts.CurrentRow.Cells[1].Value.ToString();
  88. tbxLenght.Text = dgvProducts.CurrentRow.Cells[2].Value.ToString();
  89. tbxWidth.Text = dgvProducts.CurrentRow.Cells[3].Value.ToString();
  90. }
  91. private void btnProducts_Click(object sender, EventArgs e)
  92. {
  93. }
  94. }
  95. }