Learning/Mysql-example/MainForm.cs
2023-05-05 20:58:11 +02:00

112 lines
No EOL
4.9 KiB
C#

using Mysql_example.Database;
using Mysql_example.Database.Schema;
using Mysql_example.Util;
using static Mysql_example.Database.DBConnectSpecific;
namespace Mysql_example
{
public partial class MainForm : Form
{
Dictionary<UInt64, User> users;
Dictionary<UInt64, Address> addresses;
private DataGridViewCellEventArgs mouseLocation;
public MainForm()
{
InitializeComponent();
}
private void MainPage_Load(object sender, EventArgs e)
{
LoadData();
SetupUsersDataGridView();
SetupAddressesDataGriView();
}
private void LoadData()
{
DBConnectSpecific connection = new DBConnectSpecific();
usersAndAddressesStruct result = connection.ReadUsersAndAddresses();
users = result.users;
addresses = result.addresses;
}
private void SetupUsersDataGridView()
{
DataGridViewColumn oldColumn = DataGridViewUserInfo.Columns["edit"];
if(oldColumn != null ) DataGridViewUserInfo.Columns.Remove(oldColumn);
DataGridViewUserInfo.DataSource = Utility.usersToBindingList(users);
DataGridViewUserInfo.ReadOnly= true;
//Add Edit buttons
DataGridViewButtonColumn editColumn = new DataGridViewButtonColumn();
editColumn.Name = "edit";
editColumn.Text = "Edit";
editColumn.UseColumnTextForButtonValue= true;
editColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
DataGridViewUserInfo.Columns.Add(editColumn);
editColumn.Width = 64;
editColumn.MinimumWidth = 64;
}
private void SetupAddressesDataGriView()
{
DataGridViewAdressInfo.DataSource = Utility.addressesToBindingList(addresses);
DataGridViewAdressInfo.Columns[nameof(Address.PostalCode)].Visible = false;
DataGridViewAdressInfo.Columns[nameof(Address.Region)].Visible = false;
DataGridViewAdressInfo.ReadOnly= true;
}
private void RefreshDataGridViews()
{
LoadData();
int scrollRowIndexUsers = DataGridViewUserInfo.FirstDisplayedScrollingRowIndex != -1 ? DataGridViewUserInfo.FirstDisplayedScrollingRowIndex : 0;
int scrollRowIndexAddresses = DataGridViewAdressInfo.FirstDisplayedScrollingRowIndex != -1 ? DataGridViewAdressInfo.FirstDisplayedScrollingRowIndex : 0;
SetupUsersDataGridView();
SetupAddressesDataGriView();
DataGridViewUserInfo.FirstDisplayedScrollingRowIndex = scrollRowIndexUsers;
DataGridViewAdressInfo.FirstDisplayedScrollingRowIndex = scrollRowIndexAddresses;
}
private void EditUser(User user)
{
UserEditForm editForm = new UserEditForm(ref user);
editForm.SaveUserCallback += new UserEditForm.SaveUserDelegate(this.OnUserSaveCallback);
editForm.Show();
}
private void OnUserSaveCallback(ref User user)
{
DBConnectSpecific connection = new DBConnectSpecific();
connection.UpdateUser(ref user, users);
connection.UpdateAddressesOnUser(ref user, users);
RefreshDataGridViews();
}
private void DataGridViewUserInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (DataGridViewUserInfo.Columns["edit"].Index != e.ColumnIndex || e.RowIndex < 0) return;
EditUser(DataGridViewUserInfo.Rows[e.RowIndex].DataBoundItem as User);
}
private void AddUserToolStripMenuItem_Click(object sender, EventArgs e)
{
User newUser = new User();
newUser.Addresses = new Dictionary<ulong, Address>();
newUser.Birthday = DateTime.Now;
UserEditForm editForm = new UserEditForm(ref newUser);
editForm.SaveUserCallback += new UserEditForm.SaveUserDelegate(this.OnUserSaveCallback);
editForm.Show();
editForm.TextBoxEmail_Validating(null, null);
}
private void DeleteUserToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("This isn't Revertable", "Delete User?", MessageBoxButtons.YesNo);
if (dialogResult != DialogResult.Yes) return;
DataGridViewRow row = DataGridViewUserInfo.Rows[mouseLocation.RowIndex];
User user = row.DataBoundItem as User;
DBConnectSpecific connection = new DBConnectSpecific();
connection.DeleteUser(user);
RefreshDataGridViews();
}
private void DataGridViewUserInfo_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
mouseLocation = e;
}
}
}