Загрузка данных
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace WorkTimeTracker
{
public partial class Form1 : Form
{
private List<Employee> employees = new List<Employee>();
public Form1()
{
InitializeComponent();
SetupDataGridView();
}
private void SetupDataGridView()
{
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add("Name", "ФИО");
dataGridView1.Columns.Add("Hours", "Отработанные часы");
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Введите ФИО сотрудника!");
return;
}
if (!double.TryParse(txtHours.Text, out double hours))
{
MessageBox.Show("Введите корректное количество часов!");
return;
}
if (hours < 0)
{
MessageBox.Show("Часы не могут быть отрицательными!");
return;
}
Employee emp = new Employee
{
Name = txtName.Text.Trim(), Hours = hours
};
employees.Add(emp);
UpdateDataGridView();
CalculateTotalHours();
txtName.Clear();
txtHours.Clear();
txtName.Focus();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
int index = dataGridView1.CurrentRow.Index;
employees.RemoveAt(index);
UpdateDataGridView();
CalculateTotalHours();
}
else
{
MessageBox.Show("Выберите запись для удаления!");
}
}
private void btnExport_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "XML файлы|*.xml",
FileName = "worktime.xml"
};
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
Encoding = System.Text.Encoding.UTF8
};
using (XmlWriter writer = XmlWriter.Create(saveFileDialog.FileName, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("worktime");
foreach (var emp in employees)
{
writer.WriteStartElement("employee");
writer.WriteAttributeString("name", emp.Name);
writer.WriteElementString("hours", emp.Hours.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
MessageBox.Show("Данные успешно экспортированы!");
}
catch (Exception ex)
{
MessageBox.Show("Ошибка экспорта: " + ex.Message);
}
}
}
private void btnShowReport_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "XML файлы|*.xml",
FileName = "worktime.xml"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
employees.Clear();
XmlDocument doc = new XmlDocument();
doc.Load(openFileDialog.FileName);
XmlNodeList employeeNodes = doc.SelectNodes("//employee");
foreach (XmlNode node in employeeNodes)
{
Employee emp = new Employee();
if (node.Attributes["name"] != null)
emp.Name = node.Attributes["name"].Value;
XmlNode hoursNode = node.SelectSingleNode("hours");
if (hoursNode != null)
emp.Hours = double.Parse(hoursNode.InnerText);
employees.Add(emp);
}
UpdateDataGridView();
CalculateTotalHours();
MessageBox.Show($"Загружено записей: {employees.Count}");
}
catch (Exception ex)
{
MessageBox.Show("Ошибка импорта: " + ex.Message);
}
}
}
private void UpdateDataGridView()
{
dataGridView1.Rows.Clear();
foreach (var emp in employees)
{
dataGridView1.Rows.Add(emp.Name, emp.Hours.ToString("F2"));
}
}
private void CalculateTotalHours()
{
double total = 0;
foreach (var emp in employees)
{
total += emp.Hours;
}
lblTotalHours.Text = $"Общее количество часов: {total:F2}";
}
}
}