Guest User

Untitled

a guest
Nov 12th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. public partial class Main : MetroForm
  2. {
  3. public Main()
  4. {
  5. InitializeComponent();
  6. }
  7.  
  8. private void Form1_Load(object sender, EventArgs e)
  9. {
  10. // TODO: данная строка кода позволяет загрузить данные в таблицу "comp1DataSet.Sales". При необходимости она может быть перемещена или удалена.
  11. this.salesTableAdapter.Fill(this.comp1DataSet.Sales);
  12. // TODO: данная строка кода позволяет загрузить данные в таблицу "comp1DataSet.Categories". При необходимости она может быть перемещена или удалена.
  13. this.categoriesTableAdapter.Fill(this.comp1DataSet.Categories);
  14. // TODO: данная строка кода позволяет загрузить данные в таблицу "comp1DataSet.Computers". При необходимости она может быть перемещена или удалена.
  15. this.computersTableAdapter.Fill(this.comp1DataSet.Computers);
  16. fillProducts();
  17. countRow("Computers");
  18. }
  19.  
  20. private void fillProducts()
  21. {
  22. string query = @"SELECT Computers.*, Categories.Name AS Name
  23. FROM Categories INNER JOIN Computers ON Categories.[Id] = Computers.[Id_category]";
  24. fillDataGridViewFromQuery(query);
  25. }
  26.  
  27.  
  28. private void countRow(string nameTable)
  29. {
  30. try
  31. {
  32. string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=comp1.mdb";
  33. OleDbConnection con = new OleDbConnection(connectionString);
  34. OleDbCommand com = con.CreateCommand();
  35. com.CommandText = "SELECT count(*) FROM " + nameTable;
  36. con.Open();
  37. int n = Convert.ToInt32(com.ExecuteScalar());
  38. tsslCountRows.Text = "Количество записей в таблице: " + Convert.ToString(n);
  39. con.Close();
  40. }
  41. catch (Exception ex)
  42. {
  43. MessageBox.Show(ex.Message);
  44. }
  45. }
  46.  
  47.  
  48.  
  49. private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  50. {
  51.  
  52. }
  53.  
  54. private void button1_Click(object sender, EventArgs e)
  55. {
  56.  
  57. }
  58.  
  59. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  60. {
  61.  
  62. }
  63.  
  64. private void toolStripTextBox1_Click(object sender, EventArgs e)
  65. {
  66.  
  67. }
  68.  
  69.  
  70. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  71. {
  72. Application.Exit();
  73. }
  74.  
  75. private void metroTabControl1_SelectedIndexChanged(object sender, EventArgs e)
  76. {
  77. if (metroTabControl1.SelectedIndex == 0)
  78. {
  79. countRow("Computers");
  80. }
  81. else if (metroTabControl1.SelectedIndex == 1)
  82. {
  83. countRow("categories");
  84. }
  85. else
  86. {
  87. countRow("Sales");
  88. }
  89. }
  90.  
  91. private void metroButton1_Click(object sender, EventArgs e)
  92. {
  93. for (int i = 0; i < dataGridView1.RowCount; i++)
  94. {
  95. dataGridView1.Rows[i].Selected = false;
  96. for (int j = 0; j < dataGridView1.ColumnCount; j++)
  97. if (dataGridView1.Rows[i].Cells[j].Value != null)
  98. {
  99. if (metrotextBox1.Text != " " || metrotextBox1.Text != String.Empty)
  100. {
  101. if (dataGridView1.Rows[i].Cells[j].Value.ToString().ToLower().Contains(metrotextBox1.Text.ToLower()))
  102. {
  103. if (!dataGridView1.Rows[i].Visible)
  104. {
  105. dataGridView1.Rows[i].Visible = true;
  106. }
  107. dataGridView1.Rows[i].Selected = true;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113.  
  114. for (int i = 0; i < dataGridView1.RowCount; i++)
  115. {
  116. if (dataGridView1.Rows[i].Selected != true)
  117. {
  118. dataGridView1.CurrentCell = null;
  119. if (dataGridView1.Rows[i].Cells[0].Value != null)
  120. {
  121.  
  122. dataGridView1.Rows[i].Visible = false;
  123. }
  124.  
  125. }
  126.  
  127. }
  128. }
  129.  
  130. private void metroButton4_Click(object sender, EventArgs e)
  131. {
  132. fillProducts();
  133. }
  134.  
  135.  
  136. private void toolStripButton1_Click(object sender, EventArgs e)
  137. {
  138. addcomp ac = new addcomp("Add");
  139. ac.ShowDialog();
  140. fillProducts();
  141. }
  142.  
  143. private void metrotextBox1_TextChanged(object sender, EventArgs e)
  144. {
  145.  
  146. }
  147.  
  148. private void metrotextBox1_KeyPress(object sender, KeyPressEventArgs e)
  149. {
  150.  
  151. }
  152.  
  153. private void fillDataGridViewFromQuery(string query)
  154. {
  155. try
  156. {
  157. OleDbConnection sqlconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=comp1.mdb");
  158. sqlconn.Open();
  159. OleDbDataAdapter oda = new OleDbDataAdapter(query, sqlconn);
  160. DataTable dt = new DataTable();
  161. oda.Fill(dt);
  162. dataGridView1.DataSource = dt;
  163. sqlconn.Close();
  164. }
  165. catch (Exception ex)
  166. {
  167. MessageBox.Show(ex.Message);
  168. }
  169. }
  170.  
  171.  
  172. private void toolStripButton2_Click(object sender, EventArgs e)
  173. {
  174. try
  175. {
  176. OleDbConnection sqlconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=comp1.mdb");
  177. sqlconn.Open();
  178. string query = "DELETE FROM Computers WHERE Id = " + dataGridView1.CurrentRow.Cells[0].Value.ToString() + ";";
  179. OleDbDataAdapter oda = new OleDbDataAdapter(query, sqlconn);
  180. DataTable dt = new DataTable();
  181. oda.Fill(dt);
  182. sqlconn.Close();
  183. fillProducts();
  184. }
  185. catch (Exception ex)
  186. {
  187. MessageBox.Show(ex.Message);
  188. }
  189.  
  190. }
  191.  
  192. private void metroButton2_Click(object sender, EventArgs e)
  193. {
  194. fillProducts();
  195. }
  196.  
  197. private void toolStripButton4_Click(object sender, EventArgs e)
  198. {
  199. addcomp ap = new addcomp(dataGridView1.CurrentRow.Cells[0].Value.ToString(),
  200. dataGridView1.CurrentRow.Cells[1].Value.ToString(),
  201. dataGridView1.CurrentRow.Cells[2].Value.ToString(),
  202. dataGridView1.CurrentRow.Cells[3].Value.ToString(),
  203. dataGridView1.CurrentRow.Cells[4].Value.ToString(),
  204. dataGridView1.CurrentRow.Cells[5].Value.ToString(),
  205. dataGridView1.CurrentRow.Cells[6].Value.ToString(),
  206. dataGridView1.CurrentRow.Cells[7].Value.ToString(),
  207. dataGridView1.CurrentRow.Cells[8].Value.ToString(),
  208. dataGridView1.CurrentRow.Cells[9].Value.ToString(),
  209. "Edit");
  210. ap.ShowDialog();
  211. fillProducts();
  212.  
  213. }
  214.  
  215. private void выходToolStripMenuItem_Click(object sender, EventArgs e)
  216. {
  217. Application.Exit();
  218. }
  219.  
  220. private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  221. {
  222.  
  223. }
  224.  
  225. private void отчетыToolStripMenuItem_Click(object sender, EventArgs e)
  226. {
  227. Report rp = new Report();
  228. rp.ShowDialog();
  229. }
  230.  
  231. private void toolStripButton3_Click(object sender, EventArgs e)
  232. {
  233. Basket b = new Basket(dataGridView1.CurrentRow.Cells[0].Value.ToString(),
  234. dataGridView1.CurrentRow.Cells[1].Value.ToString(),
  235. dataGridView1.CurrentRow.Cells[2].Value.ToString(),
  236. dataGridView1.CurrentRow.Cells[3].Value.ToString());
  237. b.ShowDialog();
  238.  
  239. }
  240.  
  241. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  242. {
  243. this.categoriesTableAdapter.Update(this.comp1DataSet.Categories);
  244. }
  245. }
  246.  
  247. public partial class addcomp : MetroForm
  248. {
  249. string idComputer, model, processor, videocard, hdd, memory, price, description, quantity, category, operation;
  250. private void bindingSource1_CurrentChanged(object sender, EventArgs e)
  251. {
  252.  
  253. }
  254.  
  255. public addcomp(string operation)
  256. {
  257. InitializeComponent();
  258. this.operation = operation;
  259. }
  260.  
  261. public addcomp(string idComputer, string model, string processor, string videocard, string hdd, string memory, string price, string description, string quantity, string category, string operation)
  262. {
  263. InitializeComponent();
  264. this.category = category;
  265. this.model = model;
  266. this.processor = processor;
  267. this.videocard = videocard;
  268. this.hdd = hdd;
  269. this.memory = memory;
  270. this.price = price;
  271. this.description = description;
  272. this.quantity = quantity;
  273. this.operation = operation;
  274. }
  275.  
  276.  
  277. private void metroTextBox7_Click(object sender, EventArgs e)
  278. {
  279.  
  280. }
  281.  
  282. private void metroLabel7_Click(object sender, EventArgs e)
  283. {
  284.  
  285. }
  286.  
  287. private void metroButton3_Click(object sender, EventArgs e)
  288. {
  289. metroTextBox1.Text = " ";
  290. metroTextBox2.Text = " ";
  291. metroTextBox3.Text = " ";
  292. metroTextBox4.Text = " ";
  293. metroTextBox5.Text = " ";
  294. metroTextBox6.Text = " ";
  295. metroTextBox7.Text = " ";
  296. metroTextBox8.Text = " ";
  297. metroTextBox1.Focus();
  298. }
  299.  
  300. private void addcomp_Load(object sender, EventArgs e)
  301. {
  302. // TODO: данная строка кода позволяет загрузить данные в таблицу "comp1DataSet1.Categories". При необходимости она может быть перемещена или удалена.
  303. this.computersTableAdapter2.Fill(this.comp1DataSet1.Computers);
  304. this.categoriesTableAdapter.Fill(this.comp1DataSet1.Categories);
  305. metroTextBox1.Text = model;
  306. metroTextBox2.Text = processor;
  307. metroTextBox3.Text = videocard;
  308. metroTextBox4.Text = hdd;
  309. metroTextBox5.Text = memory;
  310. metroTextBox6.Text = price;
  311. metroTextBox7.Text = description;
  312. metroTextBox8.Text = quantity;
  313. metroComboBox1.Text = category;
  314.  
  315. if (operation == "Add")
  316. {
  317. metroButton1.Text = "Добавить";
  318. }
  319. else if (operation == "Edit")
  320. {
  321. this.Text = "Изменение товара";
  322. metroButton1.Text = "Изменить";
  323. }
  324.  
  325. }
  326.  
  327. private void metroButton1_Click(object sender, EventArgs e)
  328. {
  329.  
  330. int idCategory;
  331. if (!Int32.TryParse(metroComboBox1.Text, out idCategory))
  332. {
  333. idCategory = Convert.ToInt32(metroComboBox1.SelectedValue);
  334. }
  335. else
  336. {
  337. MessageBox.Show("Недопустимая категория продукта!");
  338. return;
  339. }
  340.  
  341. string model;
  342. if (metroTextBox1.Text != "" && metroTextBox1.Text != null)
  343. {
  344. model = metroTextBox1.Text;
  345. }
  346. else
  347. {
  348. MessageBox.Show("Заполните название продукта!");
  349. return;
  350. }
  351.  
  352. string processor;
  353. if (metroTextBox2.Text != "" && metroTextBox2.Text != null)
  354. {
  355. processor = metroTextBox2.Text;
  356. }
  357. else
  358. {
  359. MessageBox.Show("Заполните название продукта!");
  360. return;
  361. }
  362.  
  363. string videocard;
  364. if (metroTextBox3.Text != "" && metroTextBox3.Text != null)
  365. {
  366. videocard = metroTextBox3.Text;
  367. }
  368. else
  369. {
  370. MessageBox.Show("Заполните название продукта!");
  371. return;
  372. }
  373.  
  374. string hdd;
  375. if (metroTextBox4.Text != "" && metroTextBox4.Text != null)
  376. {
  377. hdd = metroTextBox4.Text;
  378. }
  379. else
  380. {
  381. MessageBox.Show("Заполните название продукта!");
  382. return;
  383. }
  384.  
  385. string memory;
  386. if (metroTextBox5.Text != "" && metroTextBox5.Text != null)
  387. {
  388. memory = metroTextBox5.Text;
  389. }
  390. else
  391. {
  392. MessageBox.Show("Заполните название продукта!");
  393. return;
  394. }
  395.  
  396. int price;
  397. if (Int32.TryParse(metroTextBox6.Text, out price))
  398. {
  399. price = Convert.ToInt32(metroTextBox6.Text);
  400. }
  401. else
  402. {
  403. MessageBox.Show("Некоректная цена продукта!");
  404. return;
  405. }
  406.  
  407. int quantity;
  408. if (Int32.TryParse(metroTextBox8.Text, out price))
  409. {
  410. quantity = Convert.ToInt32(metroTextBox8.Text);
  411. }
  412. else
  413. {
  414. MessageBox.Show("Некоректная цена продукта!");
  415. return;
  416. }
  417.  
  418. string description;
  419. if (metroTextBox7.Text != "" && metroTextBox7.Text != null)
  420. {
  421. description = metroTextBox5.Text;
  422. }
  423. else
  424. {
  425. MessageBox.Show("Заполните название продукта!");
  426. return;
  427. }
  428.  
  429. try
  430. {
  431. if (operation == "Add")
  432. {
  433. computersTableAdapter2.Insert(Convert.ToInt32(idComputer), model, processor, videocard, hdd, memory, price, description, quantity, idCategory);
  434. }
  435. else if (operation == "Edit")
  436. {
  437. computersTableAdapter2.Edit(model, processor, videocard, hdd, memory, price, description, quantity, idCategory, Convert.ToInt32(idComputer));
  438. }
  439. }
  440. catch (Exception ex)
  441. {
  442. MessageBox.Show(ex.Message);
  443. }
  444. this.Close();
  445.  
  446. }
  447.  
  448. private void metroButton2_Click(object sender, EventArgs e)
  449. {
  450. this.Close();
  451. }
  452. }
Add Comment
Please, Sign In to add comment