FormSpisokMaterial.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Excel = Microsoft.Office.Interop.Excel;
  11. namespace СУБД_Швейная_фабрика
  12. {
  13. public partial class FormSpisokMaterial : Form
  14. {
  15. public FormSpisokMaterial()
  16. {
  17. InitializeComponent();
  18. }
  19. private void tkaniBindingNavigatorSaveItem_Click(object sender, EventArgs e)
  20. {
  21. this.Validate();
  22. this.tkaniBindingSource.EndEdit();
  23. this.tableAdapterManager.UpdateAll(this.fabricDeryugoDataSet);
  24. }
  25. private void FormSpisokMaterial_Load(object sender, EventArgs e)
  26. {
  27. // TODO: This line of code loads data into the 'fabricDeryugoDataSet.Furnitura' table. You can move, or remove it, as needed.
  28. this.furnituraTableAdapter.Fill(this.fabricDeryugoDataSet.Furnitura);
  29. // TODO: This line of code loads data into the 'fabricDeryugoDataSet.Tkani' table. You can move, or remove it, as needed.
  30. this.tkaniTableAdapter.Fill(this.fabricDeryugoDataSet.Tkani);
  31. }
  32. private void BtnToExcel_Click(object sender, EventArgs e)
  33. {
  34. Excel.Application exapp = new Excel.Application();
  35. // сделать приложение Excel видимым
  36. exapp.Visible = true;
  37. // открыть в Excel шаблон, находящийся в папке с exe-файлом в режиме только для чтения
  38. exapp.Workbooks.Open(Application.StartupPath + "\\Списание.xlsx", Type.Missing, true);
  39. // создать переменную list1 для связи с первым листом рабочей книги
  40. Excel.Worksheet list1 = (exapp.Worksheets.get_Item(1));
  41. int RowExcel = 24; // в Excel начинать выводить с 24 строки
  42. // перебор тканей в DataGridView
  43. for (int i = 0; i <= DgvTkan.RowCount - 1; i++)
  44. {
  45. int CountSpis = 0;
  46. try
  47. {
  48. // сколько списать i-й ткани
  49. CountSpis = Convert.ToInt32(DgvTkan.Rows[i].Cells[3].Value);
  50. }
  51. catch { }
  52. // если человек указал 0, или не указал ничего,
  53. // перейти к следующей ткани
  54. if (CountSpis == 0)
  55. continue;
  56. // если человек ввел ненулевое кол-во для списания,
  57. // добавить i-ю ткань в документ
  58. list1.get_Range("A" + RowExcel).Value = DgvTkan.Rows[i].Cells[0].Value;
  59. list1.get_Range("H" + RowExcel).Value = "см";
  60. list1.get_Range("J" + RowExcel).Value = "ткань";
  61. // количество ткани для списания
  62. list1.get_Range("L" + RowExcel).Value = CountSpis;
  63. int WidthTkan = Convert.ToInt32(DgvTkan.Rows[i].Cells[1].Value);
  64. // площадь ткани для списания
  65. list1.get_Range("N" + RowExcel).Value = CountSpis * WidthTkan;
  66. // причина списания
  67. list1.get_Range("R" + RowExcel).Value = DgvTkan.Rows[i].Cells[4].Value;
  68. RowExcel++; // перейти в Excel к следующей строке
  69. }
  70. // перебор фурнитур в DataGridView
  71. for (int i = 0; i <= DgvFurnitura.RowCount - 1; i++)
  72. {
  73. int CountSpis = 0;
  74. try
  75. {
  76. // сколько списать i-й фурнитуры
  77. CountSpis = Convert.ToInt32(DgvFurnitura.Rows[i].Cells[2].Value);
  78. }
  79. catch { }
  80. // если человек указал 0, или не указал ничего,
  81. // перейти к следующей фурнитуре
  82. if (CountSpis == 0)
  83. continue;
  84. // если человек ввел ненулевое кол-во для списания,
  85. // добавить i-ю фурнитуру в документ
  86. list1.get_Range("A" + RowExcel).Value = DgvFurnitura.Rows[i].Cells[0].Value;
  87. list1.get_Range("H" + RowExcel).Value = "шт";
  88. list1.get_Range("J" + RowExcel).Value = "фурнитура";
  89. // количество фурнитуры для списания
  90. list1.get_Range("L" + RowExcel).Value = CountSpis;
  91. // причина списания
  92. list1.get_Range("R" + RowExcel).Value = DgvFurnitura.Rows[i].Cells[3].Value;
  93. RowExcel++; // перейти в Excel к следующей строке
  94. }
  95. }
  96. private void BtnSpisMaterial_Click(object sender, EventArgs e)
  97. {
  98. DialogResult rez = MessageBox.Show("Вы уверены, что нужно выбранные материалы?",
  99. "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  100. // если пользователь отказался списать, то выйти из процедуры
  101. if (rez == DialogResult.No)
  102. return;
  103. // перебор тканей в DataGridView
  104. for (int i = 0; i <= DgvTkan.RowCount - 1; i++)
  105. {
  106. int CountSpis = 0;
  107. try
  108. {
  109. // сколько списать i-й ткани
  110. CountSpis = Convert.ToInt32(DgvTkan.Rows[i].Cells[3].Value);
  111. }
  112. catch { }
  113. // если человек указал 0, или не указал ничего,
  114. // перейти к следующей ткани
  115. if (CountSpis == 0)
  116. continue;
  117. // если человек ввел ненулевое кол-во для списания,
  118. // списать ткань
  119. int LengthTkan = Convert.ToInt32(DgvTkan.Rows[i].Cells[2].Value);
  120. // уменьшить кол-во ткани на складе на кол-во списываемой ткани
  121. LengthTkan -= CountSpis;
  122. // записать в столбец с длиной рулона ткани на складе новое значение
  123. DgvTkan.Rows[i].Cells[2].Value = LengthTkan;
  124. }
  125. tkaniBindingSource.EndEdit();
  126. this.tkaniTableAdapter.Update(this.fabricDeryugoDataSet.Tkani);
  127. // перебор фурнитур в DataGridView
  128. for (int i = 0; i <= DgvFurnitura.RowCount - 1; i++)
  129. {
  130. int CountSpis = 0;
  131. try
  132. {
  133. // сколько списать i-й фурнитуры
  134. CountSpis = Convert.ToInt32(DgvFurnitura.Rows[i].Cells[2].Value);
  135. }
  136. catch { }
  137. // если человек указал 0, или не указал ничего,
  138. // перейти к следующей фурнитуре
  139. if (CountSpis == 0)
  140. continue;
  141. // если человек ввел ненулевое кол-во для списания,
  142. // списать фурнитуру
  143. // если человек ввел ненулевое кол-во для списания,
  144. // списать ткань
  145. int CountFur = Convert.ToInt32(DgvFurnitura.Rows[i].Cells[1].Value);
  146. // уменьшить кол-во фурнитур на складе на кол-во списываемых фурнитур
  147. CountFur -= CountSpis;
  148. // записать в столбец с кол-вом фурнитур на складе новое значение
  149. DgvFurnitura.Rows[i].Cells[1].Value = CountFur;
  150. }
  151. furnituraBindingSource.EndEdit();
  152. this.furnituraTableAdapter.Update(this.fabricDeryugoDataSet.Furnitura);
  153. }
  154. }
  155. }