Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. public partial class Form1 : Form
  5. {
  6. public Form1()
  7. {
  8. InitializeComponent();
  9.  
  10. //Загружаем список таблиц в comboBox
  11. comboBox1.Items.Clear();
  12. foreach(String[] line in Database.getTablesList().data){
  13. comboBox1.Items.Add(line[0]);
  14. }
  15. comboBox1.SelectionChangeCommitted += (object sender, EventArgs e) => loadTable(comboBox1.Text);
  16.  
  17. //Кнопка запроса
  18. queryBtn.Click += (object sender, EventArgs e) => {
  19. try{
  20. comboBox1.SelectedIndex = -1;
  21. clearTable();
  22. setTableData(Database.query(textBox1.Text));
  23. }catch(Exception ex){
  24. MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false);
  25. }
  26. };
  27.  
  28. //Кнопка редактирования
  29. editBtn.Click += (object sender, EventArgs e) => {
  30. if(dataGridView1.CurrentRow == null){
  31. MessageBox.Show("Сначала выберите строку для редактирования", "Ошибка");
  32. return;
  33. }
  34. Form2 form = new Form2(currentTable.columns);
  35.  
  36. foreach(DataGridViewCell cell in dataGridView1.CurrentRow.Cells){
  37. TextBox field = form.getElementForField(cell.OwningColumn.HeaderText);
  38. if(field != null){
  39. field.Text = cell.Value as String;
  40. }
  41. }
  42.  
  43. form.button.Text = "Сохранить изменения";
  44. form.button.Click += (object sndr, EventArgs ea) => {
  45. try{
  46. Database.updateData(comboBox1.Text, currentTable.columns, dataGridView1.CurrentRow, form.getValues());
  47. }catch(Exception ex){
  48. MessageBox.Show(ex.Message, "Ошибка");
  49. }
  50. form.Hide();
  51. loadTable(comboBox1.Text);
  52. };
  53.  
  54. form.Show();
  55. };
  56.  
  57. //Кнопка добавления
  58. addBtn.Click += (object sender, EventArgs e) => {
  59. Form2 form = new Form2(currentTable.columns);
  60.  
  61. form.button.Text = "Добавить запись";
  62. form.button.Click += (object sndr, EventArgs ea) => {
  63. try{
  64. Database.insertData(comboBox1.Text, currentTable.columns, form.getValues());
  65. }catch(Exception ex){
  66. MessageBox.Show(ex.Message, "Ошибка");
  67. }
  68. form.Hide();
  69. loadTable(comboBox1.Text);
  70. };
  71.  
  72. form.Show();
  73. };
  74.  
  75. //Кнопка удаления
  76. delBtn.Click += (object sender, EventArgs e) => {
  77. if(dataGridView1.CurrentRow == null){
  78. MessageBox.Show("Сначала выберите строку для удаления", "Ошибка");
  79. return;
  80. }
  81. try{
  82. Database.deleteData(comboBox1.Text, currentTable.columns, dataGridView1.CurrentRow);
  83. }catch(Exception ex){
  84. MessageBox.Show(ex.Message, "Ошибка");
  85. }
  86. };
  87.  
  88. }
  89.  
  90. public void loadTable(String tableName){
  91. textBox1.Text = "SELECT * FROM `"+tableName+"`";
  92.  
  93. clearTable();
  94. setTableData(Database.query(textBox1.Text));
  95.  
  96. addBtn.Enabled = true;
  97. editBtn.Enabled = true;
  98. delBtn.Enabled = true;
  99. }
  100.  
  101. private QueryResult currentTable = null;
  102.  
  103. public void clearTable(){
  104. dataGridView1.Columns.Clear();
  105. dataGridView1.Rows.Clear();
  106. currentTable = null;
  107.  
  108. addBtn.Enabled = false;
  109. editBtn.Enabled = false;
  110. delBtn.Enabled = false;
  111. }
  112.  
  113. public void setTableData(QueryResult table){
  114.  
  115. clearTable();
  116.  
  117. foreach(String columnName in table.columns.Keys){
  118. DataGridViewColumn column = new DataGridViewTextBoxColumn();
  119. column.HeaderText = columnName;
  120. dataGridView1.Columns.Add(column);
  121. }
  122.  
  123. foreach(String[] line in table.data){
  124. dataGridView1.Rows.Add(line);
  125. }
  126.  
  127. currentTable = table;
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement