Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.54 KB | None | 0 0
  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 CsvParser;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Theaters;
  12. using System.IO;
  13. using System.Reflection;
  14.  
  15. namespace KDZ_norm
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. public Form1(List<Theater> list)
  24. {
  25. InitializeComponent();
  26. currentList = list;
  27. csv.Rows = ListOfLists(currentList);
  28. FullDaGrid(currentList, (int)numericUpDown1.Value);
  29. }
  30. /// <summary>
  31. /// С помощью этого открываем/храним/записываем csv файл
  32. /// </summary>
  33. public static Csv csv = new Csv();
  34. /// <summary>
  35. /// Здесь храним текущий список объектов типа Theater
  36. /// </summary>
  37. public List<Theater> currentList = new List<Theater>();
  38. /// <summary>
  39. /// метод для заполнения dataGridView1
  40. /// </summary>
  41. /// <param name="theaters">текущий список объектов</param>
  42. /// <param name="length">количество столбцов,выбранных пользователем</param>
  43. public void FullDaGrid(List<Theater> theaters, int length)
  44. {
  45. try
  46. {
  47. if (length == 0) { length = 22; numericUpDown1.Value = 22; }
  48. dataGridView1.ColumnCount = length;
  49. dataGridView1.RowCount = 0;
  50. FillGridWithHeaders(length);
  51. if (theaters != null)
  52. {
  53. for (int i = 0; i < theaters.Count; i++)
  54. {
  55. dataGridView1.Rows.Add(theaters[i].ROWNUM, theaters[i].CommonName, theaters[i].FullName,
  56. theaters[i].ShortName, theaters[i].location.AdmArea, theaters[i].location.District,
  57. theaters[i].location.Address, theaters[i].ChiefName, theaters[i].ChiefPosition,
  58. theaters[i].PublicPhone, theaters[i].Fax, theaters[i].Email,
  59. theaters[i].WorkingHours, theaters[i].ClarificationOfWorkingHours, theaters[i].WebSite,
  60. theaters[i].OKPO, theaters[i].INN, theaters[i].MainHall, theaters[i].AdditionalHallCapacity,
  61. theaters[i].location.X_WGS, theaters[i].location.Y_WGS, theaters[i].GLOBALID);
  62. }
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. MessageBox.Show(ex.Message);
  68. }
  69. }
  70. /// <summary>
  71. /// заполнение dataGridView заголовками
  72. /// </summary>
  73. /// <param name="length"></param>
  74. public void FillGridWithHeaders(int length)
  75. {
  76. string[] columns = new string[22] { "ROWNUM","CommonName", "FullName", "ShortName", "AdmArea", "District", "Address" , "ChiefName", "ChiefPosition",
  77. "PublicPhone","Fax","Email","WorkingHours","WorkingHours","WebSite","OKPO","INN","MainHall","AdditionalHallCapacity","X_WGS","Y_WGS","GLOBALID"};
  78. for (int i = 0; i < length; i++)
  79. {
  80. dataGridView1.Columns[i].HeaderText = columns[i];
  81. }
  82. }
  83. /// <summary>
  84. /// Метод для сохранения в csv различными способами
  85. /// </summary>
  86. /// <param name="sender"></param>
  87. /// <param name="e"></param>
  88. private void SaveFile_Click(object sender, EventArgs e)
  89. {
  90. MessageBox.Show("Пожалуйста,убедитесь,что файл закрыт,иначе сохранения не прозойдёт");
  91. try
  92. {
  93. SaveFileDialog sfd = new SaveFileDialog();
  94. sfd.Filter = "CSV Files (*.csv)|*.csv";
  95. if (sfd.ShowDialog() == DialogResult.OK)
  96. {
  97. switch (listBox1.SelectedIndex)
  98. {
  99. case 0:
  100. case -1:
  101. {
  102. csv.HowFileSave(sfd.FileName, true);
  103. break;
  104. }
  105. case 1:
  106. {
  107. csv.HowFileSave(sfd.FileName, false);
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show(ex.Message);
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// Метод для преобразования из списка объектов в "двумерный массив",если так можно выразиться
  121. /// </summary>
  122. /// <param name="theaters">"Двумерный массив элементов dataGridView1</param>
  123. /// <returns></returns>
  124. public List<List<string>> ListOfLists(List<Theater> theaters)
  125. {
  126. List<List<string>> newList = new List<List<string>>();
  127. try
  128. {
  129. for (int i = 0; i < theaters.Count; i++)
  130. {
  131. newList.Add(new List<string> { theaters[i].ROWNUM, theaters[i].CommonName, theaters[i].FullName,
  132. theaters[i].ShortName, theaters[i].location.AdmArea, theaters[i].location.District,
  133. theaters[i].location.Address, theaters[i].ChiefName, theaters[i].ChiefPosition,
  134. theaters[i].PublicPhone, theaters[i].Fax, theaters[i].Email,
  135. theaters[i].WorkingHours, theaters[i].ClarificationOfWorkingHours, theaters[i].WebSite,
  136. theaters[i].OKPO, theaters[i].INN, theaters[i].MainHall, theaters[i].AdditionalHallCapacity,
  137. theaters[i].location.X_WGS, theaters[i].location.Y_WGS, theaters[i].GLOBALID});
  138.  
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. MessageBox.Show(ex.Message);
  144. }
  145. return newList;
  146. }
  147. /// <summary>
  148. /// Открываем Csv файл
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void OpenFile_Click(object sender, EventArgs e)
  153. {
  154. try
  155. {
  156. currentList.Clear();
  157. OpenFileDialog ofd = new OpenFileDialog();
  158. ofd.Filter = "CSV Files (*.csv)|*.csv";
  159. if (ofd.ShowDialog() == DialogResult.OK)
  160. {
  161. csv.FileOpen(ofd.FileName);
  162. currentList = Theater.CreateObjects(csv.Rows);
  163. FullDaGrid(currentList, (int)numericUpDown1.Value);
  164. }
  165. if (dataGridView1.Rows.Count == 0) MessageBox.Show("По всей видимости,вы открыли файл,не предназначенный для данной программы,либо же просто пустой файл");
  166. }
  167. catch (Exception ex)
  168. {
  169. MessageBox.Show(ex.Message);
  170. }
  171. MessageBox.Show("ВНИМАНИЕ!\nПри вводе некорректных данных ячейка будет очищена!");
  172. }
  173. /// <summary>
  174. /// событие изменения показываемых строк и заполнение таблицы
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  179. {
  180. FullDaGrid(currentList, (int)numericUpDown1.Value);
  181. }
  182. /// <summary>
  183. /// контроль изменений в ячейках
  184. /// </summary>
  185. /// <param name="sender"></param>
  186. /// <param name="e"></param>
  187. private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  188. {
  189. try
  190. {
  191. if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
  192. {
  193. csv.Rows[e.RowIndex][e.ColumnIndex] = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
  194. currentList = Theater.CreateObjects(csv.Rows);
  195. csv.Rows = ListOfLists(currentList);
  196. FullDaGrid(currentList, (int)numericUpDown1.Value);
  197. }
  198. }
  199. catch(Exception ex)
  200. {
  201. MessageBox.Show(ex.Message);
  202. }
  203. }
  204. /// <summary>
  205. /// Сортировка по полю CommonName
  206. /// </summary>
  207. /// <param name="sender"></param>
  208. /// <param name="e"></param>
  209. private void CommonNameSort_Click(object sender, EventArgs e)
  210. {
  211. currentList = currentList.OrderBy(o => o.CommonName).ToList();
  212. csv.Rows = ListOfLists(currentList);
  213. FullDaGrid(currentList, (int)numericUpDown1.Value);
  214. }
  215. /// <summary>
  216. /// Сортировка по полю MainHall
  217. /// </summary>
  218. /// <param name="sender"></param>
  219. /// <param name="e"></param>
  220. private void MainHallCapacitySort_Click(object sender, EventArgs e)
  221. {
  222. currentList = currentList.OrderBy(o => o.intMainHall).ToList();
  223. csv.Rows = ListOfLists(currentList);
  224. FullDaGrid(currentList, (int)numericUpDown1.Value);
  225. }
  226. /// <summary>
  227. /// Сортировка по полю ChiefName
  228. /// </summary>
  229. /// <param name="sender"></param>
  230. /// <param name="e"></param>
  231. private void ChiefNameSort_Click(object sender, EventArgs e)
  232. {
  233. currentList = currentList.OrderBy(o => o.ChiefName).ToList();
  234. csv.Rows = ListOfLists(currentList);
  235. FullDaGrid(currentList, (int)numericUpDown1.Value);
  236. }
  237. /// <summary>
  238. /// Сортировка по суммарному количеству человек в главном и дополнительном залах
  239. /// </summary>
  240. /// <param name="sender"></param>
  241. /// <param name="e"></param>
  242. private void AllCapacitySort_Click(object sender, EventArgs e)
  243. {
  244. currentList = currentList.OrderBy(o => o.GetAllCapacity()).ToList();
  245. csv.Rows = ListOfLists(currentList);
  246. FullDaGrid(currentList, (int)numericUpDown1.Value);
  247. }
  248. /// <summary>
  249. /// Метод удаления выбранных записей о театре
  250. /// </summary>
  251. /// <param name="sender"></param>
  252. /// <param name="e"></param>
  253. private void DeleteRow_Click(object sender, EventArgs e)
  254. {
  255. try
  256. {
  257. foreach (DataGridViewRow row in dataGridView1.SelectedRows)
  258. {
  259. currentList.RemoveAt(row.Index);
  260. csv.Rows = ListOfLists(currentList);
  261. dataGridView1.Rows.Remove(row);
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. MessageBox.Show(ex.Message);
  267. }
  268. }
  269. /// <summary>
  270. /// Создаём новую запись о театре
  271. /// </summary>
  272. /// <param name="sender"></param>
  273. /// <param name="e"></param>
  274. private void AddNewRow_Click(object sender, EventArgs e)
  275. {
  276. NewRowAdding newRowAdding = new NewRowAdding(currentList);
  277. newRowAdding.Show();
  278. this.Hide();
  279. }
  280. /// <summary>
  281. /// Очистка текущего списка объектов и возможность создания пользователем своего списка
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private void ClearList_Click(object sender, EventArgs e)
  286. {
  287. currentList.Clear();
  288. csv.Rows = ListOfLists(currentList);
  289. FullDaGrid(currentList, (int)numericUpDown1.Value);
  290. }
  291. /// <summary>
  292. /// поднять настройки(настроение)
  293. /// </summary>
  294. /// <param name="sender"></param>
  295. /// <param name="e"></param>
  296. private void settingsShow_Click(object sender, EventArgs e)
  297. {
  298. Settings settings = new Settings();
  299. settings.Show();
  300. }
  301. private void Form1_Load(object sender, EventArgs e)
  302. {
  303. string[] lines = new string[2] { "Добавить в файл", "Заменить файл" };
  304. listBox1.Items.AddRange(lines);
  305. }
  306. /// <summary>
  307. /// Сортировка по полю AdmArea
  308. /// </summary>
  309. /// <param name="sender"></param>
  310. /// <param name="e"></param>
  311. private void AdmAreaSort_Click(object sender, EventArgs e)
  312. {
  313. currentList = currentList.OrderBy(o => o.location.AdmArea).ToList();
  314. csv.Rows = ListOfLists(currentList);
  315. FullDaGrid(currentList, (int)numericUpDown1.Value);
  316. }
  317. }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement