Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using CsvParser;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Theaters;
- using System.IO;
- using System.Reflection;
- namespace KDZ_norm
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public Form1(List<Theater> list)
- {
- InitializeComponent();
- currentList = list;
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// С помощью этого открываем/храним/записываем csv файл
- /// </summary>
- public static Csv csv = new Csv();
- /// <summary>
- /// Здесь храним текущий список объектов типа Theater
- /// </summary>
- public List<Theater> currentList = new List<Theater>();
- /// <summary>
- /// метод для заполнения dataGridView1
- /// </summary>
- /// <param name="theaters">текущий список объектов</param>
- /// <param name="length">количество столбцов,выбранных пользователем</param>
- public void FullDaGrid(List<Theater> theaters, int length)
- {
- try
- {
- if (length == 0) { length = 22; numericUpDown1.Value = 22; }
- dataGridView1.ColumnCount = length;
- dataGridView1.RowCount = 0;
- FillGridWithHeaders(length);
- if (theaters != null)
- {
- for (int i = 0; i < theaters.Count; i++)
- {
- dataGridView1.Rows.Add(theaters[i].ROWNUM, theaters[i].CommonName, theaters[i].FullName,
- theaters[i].ShortName, theaters[i].location.AdmArea, theaters[i].location.District,
- theaters[i].location.Address, theaters[i].ChiefName, theaters[i].ChiefPosition,
- theaters[i].PublicPhone, theaters[i].Fax, theaters[i].Email,
- theaters[i].WorkingHours, theaters[i].ClarificationOfWorkingHours, theaters[i].WebSite,
- theaters[i].OKPO, theaters[i].INN, theaters[i].MainHall, theaters[i].AdditionalHallCapacity,
- theaters[i].location.X_WGS, theaters[i].location.Y_WGS, theaters[i].GLOBALID);
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// заполнение dataGridView заголовками
- /// </summary>
- /// <param name="length"></param>
- public void FillGridWithHeaders(int length)
- {
- string[] columns = new string[22] { "ROWNUM","CommonName", "FullName", "ShortName", "AdmArea", "District", "Address" , "ChiefName", "ChiefPosition",
- "PublicPhone","Fax","Email","WorkingHours","WorkingHours","WebSite","OKPO","INN","MainHall","AdditionalHallCapacity","X_WGS","Y_WGS","GLOBALID"};
- for (int i = 0; i < length; i++)
- {
- dataGridView1.Columns[i].HeaderText = columns[i];
- }
- }
- /// <summary>
- /// Метод для сохранения в csv различными способами
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SaveFile_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Пожалуйста,убедитесь,что файл закрыт,иначе сохранения не прозойдёт");
- try
- {
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "CSV Files (*.csv)|*.csv";
- if (sfd.ShowDialog() == DialogResult.OK)
- {
- switch (listBox1.SelectedIndex)
- {
- case 0:
- case -1:
- {
- csv.HowFileSave(sfd.FileName, true);
- break;
- }
- case 1:
- {
- csv.HowFileSave(sfd.FileName, false);
- break;
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// Метод для преобразования из списка объектов в "двумерный массив",если так можно выразиться
- /// </summary>
- /// <param name="theaters">"Двумерный массив элементов dataGridView1</param>
- /// <returns></returns>
- public List<List<string>> ListOfLists(List<Theater> theaters)
- {
- List<List<string>> newList = new List<List<string>>();
- try
- {
- for (int i = 0; i < theaters.Count; i++)
- {
- newList.Add(new List<string> { theaters[i].ROWNUM, theaters[i].CommonName, theaters[i].FullName,
- theaters[i].ShortName, theaters[i].location.AdmArea, theaters[i].location.District,
- theaters[i].location.Address, theaters[i].ChiefName, theaters[i].ChiefPosition,
- theaters[i].PublicPhone, theaters[i].Fax, theaters[i].Email,
- theaters[i].WorkingHours, theaters[i].ClarificationOfWorkingHours, theaters[i].WebSite,
- theaters[i].OKPO, theaters[i].INN, theaters[i].MainHall, theaters[i].AdditionalHallCapacity,
- theaters[i].location.X_WGS, theaters[i].location.Y_WGS, theaters[i].GLOBALID});
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- return newList;
- }
- /// <summary>
- /// Открываем Csv файл
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OpenFile_Click(object sender, EventArgs e)
- {
- try
- {
- currentList.Clear();
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "CSV Files (*.csv)|*.csv";
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- csv.FileOpen(ofd.FileName);
- currentList = Theater.CreateObjects(csv.Rows);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- if (dataGridView1.Rows.Count == 0) MessageBox.Show("По всей видимости,вы открыли файл,не предназначенный для данной программы,либо же просто пустой файл");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- MessageBox.Show("ВНИМАНИЕ!\nПри вводе некорректных данных ячейка будет очищена!");
- }
- /// <summary>
- /// событие изменения показываемых строк и заполнение таблицы
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void numericUpDown1_ValueChanged(object sender, EventArgs e)
- {
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// контроль изменений в ячейках
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- try
- {
- if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
- {
- csv.Rows[e.RowIndex][e.ColumnIndex] = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
- currentList = Theater.CreateObjects(csv.Rows);
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// Сортировка по полю CommonName
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CommonNameSort_Click(object sender, EventArgs e)
- {
- currentList = currentList.OrderBy(o => o.CommonName).ToList();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// Сортировка по полю MainHall
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MainHallCapacitySort_Click(object sender, EventArgs e)
- {
- currentList = currentList.OrderBy(o => o.intMainHall).ToList();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// Сортировка по полю ChiefName
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ChiefNameSort_Click(object sender, EventArgs e)
- {
- currentList = currentList.OrderBy(o => o.ChiefName).ToList();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// Сортировка по суммарному количеству человек в главном и дополнительном залах
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AllCapacitySort_Click(object sender, EventArgs e)
- {
- currentList = currentList.OrderBy(o => o.GetAllCapacity()).ToList();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// Метод удаления выбранных записей о театре
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DeleteRow_Click(object sender, EventArgs e)
- {
- try
- {
- foreach (DataGridViewRow row in dataGridView1.SelectedRows)
- {
- currentList.RemoveAt(row.Index);
- csv.Rows = ListOfLists(currentList);
- dataGridView1.Rows.Remove(row);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// Создаём новую запись о театре
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AddNewRow_Click(object sender, EventArgs e)
- {
- NewRowAdding newRowAdding = new NewRowAdding(currentList);
- newRowAdding.Show();
- this.Hide();
- }
- /// <summary>
- /// Очистка текущего списка объектов и возможность создания пользователем своего списка
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ClearList_Click(object sender, EventArgs e)
- {
- currentList.Clear();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- /// <summary>
- /// поднять настройки(настроение)
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void settingsShow_Click(object sender, EventArgs e)
- {
- Settings settings = new Settings();
- settings.Show();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- string[] lines = new string[2] { "Добавить в файл", "Заменить файл" };
- listBox1.Items.AddRange(lines);
- }
- /// <summary>
- /// Сортировка по полю AdmArea
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AdmAreaSort_Click(object sender, EventArgs e)
- {
- currentList = currentList.OrderBy(o => o.location.AdmArea).ToList();
- csv.Rows = ListOfLists(currentList);
- FullDaGrid(currentList, (int)numericUpDown1.Value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement