286 lines
12 KiB
Plaintext
286 lines
12 KiB
Plaintext
using Microsoft.Data.Sqlite;
|
|
using System.ComponentModel;
|
|
using trakker.Data;
|
|
using trakker.Forms;
|
|
using trakker.Interfaces;
|
|
using trakker.Models;
|
|
using trakker.Services;
|
|
|
|
namespace trakker
|
|
{
|
|
public partial class MainForm : Form, IMainForm
|
|
{
|
|
//private readonly string _dbversion = "[N.N.N]";
|
|
private string connectionString = string.Empty;
|
|
readonly MainCtrl _ctrl;
|
|
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MainForm"/> class.
|
|
/// Sets up the form's controls and event handlers by calling
|
|
/// <see cref="InitializeComponent"/> which is generated by the designer.
|
|
/// </summary>
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// build connection string that will be used for database connections
|
|
// ------------------------------------------------------------------------
|
|
var dbPath = Path.Combine(AppContext.BaseDirectory, "trakker.db");
|
|
connectionString = new SqliteConnectionStringBuilder
|
|
{
|
|
DataSource = dbPath,
|
|
Mode = SqliteOpenMode.ReadWriteCreate,
|
|
Cache = SqliteCacheMode.Shared
|
|
}.ToString();
|
|
|
|
tabControlMainForm.TabPages[0].Text = " Home ";
|
|
tabControlMainForm.TabPages[1].Text = " Clients ";
|
|
tabControlMainForm.TabPages[2].Text = " Projects ";
|
|
|
|
_ctrl = new Services.MainCtrl(this, connectionString);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the Click event of the Exit menu item. When invoked, this
|
|
/// method terminates the application.
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">Event data associated with the click event.</param>
|
|
private void MainForm_Exit_MenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
|
|
public void InitDataGridViewClients(BindingList<Client> clients)
|
|
{
|
|
dataGridViewClients.AllowUserToAddRows = true;
|
|
dataGridViewClients.AllowUserToDeleteRows = true;
|
|
dataGridViewClients.AutoGenerateColumns = false;
|
|
dataGridViewClients.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
|
dataGridViewClients.BackgroundColor = Color.White;
|
|
dataGridViewClients.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
|
dataGridViewClients.RowHeadersVisible = false;
|
|
dataGridViewClients.ColumnHeadersVisible = true;
|
|
dataGridViewClients.MultiSelect = false;
|
|
dataGridViewClients.DataSource = clients;
|
|
|
|
dataGridViewClients.Columns.Clear();
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Name",
|
|
Name = "Name",
|
|
Visible = true,
|
|
};
|
|
dataGridViewClients.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Company",
|
|
Name = "Company",
|
|
Visible = true,
|
|
};
|
|
dataGridViewClients.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Email",
|
|
Name = "Email",
|
|
Visible = true,
|
|
};
|
|
dataGridViewClients.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Phone",
|
|
Name = "Phone",
|
|
Visible = true,
|
|
};
|
|
dataGridViewClients.Columns.Add(textColumn);
|
|
}
|
|
|
|
dataGridViewClients.DoubleClick += (s, e) =>
|
|
{
|
|
if (dataGridViewClients.SelectedRows.Count > 0)
|
|
{
|
|
var selectedClient = dataGridViewClients.SelectedRows[0].DataBoundItem as Client;
|
|
if (selectedClient != null)
|
|
{
|
|
var dialog = new ClientForm(selectedClient);
|
|
if (dialog.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
Client client = dialog.Client;
|
|
ClientData clientData = new ClientData(connectionString);
|
|
try
|
|
{
|
|
clientData.Upsert(client);
|
|
dataGridViewClients.Refresh(); // Refresh the DataGridView to reflect changes
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error saving client: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
dataGridViewClients.SelectionChanged += (s, e) =>
|
|
{
|
|
if (dataGridViewClients.SelectedRows.Count > 0)
|
|
{
|
|
var selectedClient = dataGridViewClients.SelectedRows[0].DataBoundItem as Client;
|
|
if (selectedClient != null)
|
|
{
|
|
// Handle the selected client as needed
|
|
// MessageBox.Show($"Selected Client: {selectedClient.AddressStreet}", "Client Selected", MessageBoxButtons.OK, MessageBoxIcon.Information );
|
|
}
|
|
}
|
|
};
|
|
|
|
}
|
|
public void InitDataGridViewProjects(BindingList<Project> projects)
|
|
{
|
|
dataGridViewProjects.AllowUserToAddRows = true;
|
|
dataGridViewProjects.AllowUserToDeleteRows = true;
|
|
dataGridViewProjects.AutoGenerateColumns = false;
|
|
dataGridViewProjects.BackgroundColor = Color.White;
|
|
dataGridViewProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
|
dataGridViewProjects.RowHeadersVisible = false;
|
|
dataGridViewProjects.ColumnHeadersVisible = true;
|
|
dataGridViewProjects.MultiSelect = false;
|
|
dataGridViewProjects.DataSource = projects;
|
|
dataGridViewProjects.Columns.Clear();
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "ProjectName",
|
|
Name = "Project Name",
|
|
Visible = true,
|
|
};
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "ClientName",
|
|
Name = "Client Name",
|
|
Visible = true,
|
|
};
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "StartDate",
|
|
Name = "Start Date",
|
|
Visible = true,
|
|
};
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "EndDate",
|
|
Name = "End Date",
|
|
Visible = true,
|
|
};
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Status",
|
|
Name = "Status",
|
|
Visible = true,
|
|
};
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
{
|
|
var textColumn = new DataGridViewTextBoxColumn
|
|
{
|
|
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
|
DataPropertyName = "Budget",
|
|
Name = "Budget",
|
|
Visible = true,
|
|
};
|
|
textColumn.DefaultCellStyle.Format = "$#,##0.00";
|
|
textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
|
|
textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
|
|
dataGridViewProjects.Columns.Add(textColumn);
|
|
}
|
|
|
|
dataGridViewProjects.DoubleClick += (s, e) =>
|
|
{
|
|
if (dataGridViewProjects.SelectedRows.Count > 0)
|
|
{
|
|
var selectedProject = dataGridViewProjects.SelectedRows[0].DataBoundItem as Project;
|
|
if (selectedProject != null)
|
|
{
|
|
var dialog = new ProjectForm(selectedProject);
|
|
if (dialog.ShowDialog(this) == DialogResult.OK)
|
|
{
|
|
// Project project = dialog.Project;
|
|
// ProjectData projectData = new ProjectData(connectionString);
|
|
// try
|
|
// {
|
|
// projectData.Upsert(project);
|
|
// dataGridViewProjects.Refresh(); // Refresh the DataGridView to reflect changes
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MessageBox.Show($"Error saving project: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
// }
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
dataGridViewClients.SelectionChanged += (s, e) =>
|
|
{
|
|
if (dataGridViewClients.SelectedRows.Count > 0)
|
|
{
|
|
var selectedClient = dataGridViewClients.SelectedRows[0].DataBoundItem as Client;
|
|
if (selectedClient != null)
|
|
{
|
|
// Handle the selected client as needed
|
|
// MessageBox.Show($"Selected Client: {selectedClient.AddressStreet}", "Client Selected", MessageBoxButtons.OK, MessageBoxIcon.Information );
|
|
}
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
//private void button1_Click(object sender, EventArgs e)
|
|
//{
|
|
// var dialog = new ClientForm(new Client());
|
|
// if (dialog.ShowDialog(this) == DialogResult.OK)
|
|
// {
|
|
// Client client = dialog.Client;
|
|
// ClientData clientData = new ClientData(connectionString);
|
|
// try
|
|
// {
|
|
// clientData.Upsert(client);
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MessageBox.Show($"Error saving client: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
// }
|
|
// }
|
|
//}
|
|
}
|
|
}
|