VidNomenkatury.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace proba
  12. {
  13. public partial class VidNomenkatury : Form
  14. {
  15. string id_view_tools, view_tools;
  16. DataBase dataBase = new DataBase();
  17. public VidNomenkatury()
  18. {
  19. InitializeComponent();
  20. }
  21. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  22. {
  23. dgw.Rows.Add(record.GetInt32(0), record.GetString(1),RowState.ModifiedNew);
  24. }
  25. private void RefresDataGird(DataGridView dgw)
  26. {
  27. dgw.Rows.Clear();
  28. string queryString = $"Select * from View_tools";
  29. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  30. dataBase.openConnection();
  31. SqlDataReader reader = command.ExecuteReader();
  32. while (reader.Read())
  33. {
  34. ReadSingleRow(dgw, reader);
  35. }
  36. reader.Close();
  37. }
  38. private void CreateColumns()
  39. {
  40. dataGridView1.Columns.Add("id_view_tools", "Номер виде инструмента");
  41. dataGridView1.Columns.Add("view_tools", "Вид инструмента");
  42. }
  43. private void VidNomenkatury_Load(object sender, EventArgs e)
  44. {
  45. CreateColumns();
  46. RefresDataGird(dataGridView1);
  47. }
  48. private void button_back_Click(object sender, EventArgs e)
  49. {
  50. this.Hide();
  51. Nomenklatura nomenklatura = new Nomenklatura();
  52. nomenklatura.ShowDialog();
  53. }
  54. private void pictureBox2_Click(object sender, EventArgs e)
  55. {
  56. RefresDataGird(dataGridView1);
  57. }
  58. private void button_add_Click(object sender, EventArgs e)
  59. {
  60. this.Hide();
  61. VidNomenklatury_add vidNomenkatury_add = new VidNomenklatury_add();
  62. vidNomenkatury_add.ShowDialog();
  63. }
  64. private void Search(DataGridView dgw)
  65. {
  66. dgw.Rows.Clear();
  67. string searchString = $"Select * from View_tools Where view_tools like '%" + StrokaSearch.Text + "%'";
  68. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  69. dataBase.openConnection();
  70. SqlDataReader read = com.ExecuteReader();
  71. while (read.Read())
  72. {
  73. ReadSingleRow(dgw, read);
  74. }
  75. read.Close();
  76. }
  77. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  78. {
  79. Search(dataGridView1);
  80. }
  81. //private void button_insert_Click(object sender, EventArgs e)
  82. //{
  83. // if (id_view_tools == null)
  84. // {
  85. // MessageBox.Show("Не выделена строчка для изменения");
  86. // }
  87. // else
  88. // {
  89. // this.Hide();
  90. // Vid nomenklatura_Update = new Nomenklatura_update(id_tools, id_view_tools, name, description, price, id_suplier, data_manufacture, id_country);
  91. // nomenklatura_Update.ShowDialog();
  92. // }
  93. //}
  94. private void deleteRow()
  95. {
  96. int index = dataGridView1.CurrentCell.RowIndex;
  97. dataGridView1.Rows[index].Visible = false;
  98. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  99. {
  100. dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted;
  101. return;
  102. }
  103. dataGridView1.Rows[index].Cells[1].Value = RowState.Deleted;
  104. }
  105. private void Update()
  106. {
  107. dataBase.openConnection();
  108. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  109. {
  110. var rowState = (RowState)dataGridView1.Rows[index].Cells[1].Value;
  111. if (rowState == RowState.Existed)
  112. continue;
  113. if (rowState == RowState.Deleted)
  114. {
  115. var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
  116. var deleteQuery = $"delete from View_tools where id_view_tools = {id}";
  117. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  118. command.ExecuteNonQuery();
  119. }
  120. }
  121. dataBase.closeConnection();
  122. }
  123. private void button_delete_Click(object sender, EventArgs e)
  124. {
  125. deleteRow();
  126. }
  127. private void button1_Click(object sender, EventArgs e)
  128. {
  129. Update();
  130. }
  131. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  132. {
  133. }
  134. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  135. {
  136. var selectedRow = e.RowIndex;
  137. if (e.RowIndex >= 0)
  138. {
  139. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  140. id_view_tools = row.Cells[0].Value.ToString();
  141. view_tools = row.Cells[1].Value.ToString();
  142. }
  143. }
  144. private void button_insert_Click(object sender, EventArgs e)
  145. {
  146. this.Hide();
  147. VidNomenklaturyUpdate vidNomenklaturyUpdate = new VidNomenklaturyUpdate(id_view_tools, view_tools);
  148. vidNomenklaturyUpdate.ShowDialog();
  149. }
  150. }
  151. }