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; /// /// Initializes a new instance of the class. /// Sets up the form's controls and event handlers by calling /// which is generated by the designer. /// 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); } /// /// Handles the Click event of the Exit menu item. When invoked, this /// method terminates the application. /// /// The source of the event. /// Event data associated with the click event. private void MainForm_Exit_MenuItem_Click(object sender, EventArgs e) { Application.Exit(); } public void InitDataGridViewClients(BindingList 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); } } } } }