trakker/Forms/MainForm.cs

169 lines
6.6 KiB
C#

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 ";
_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 );
}
}
};
}
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);
}
}
}
}
}