git_bsmd/CoreDeleteTool/Main.cs

127 lines
4.9 KiB
C#

using System;
using System.Windows.Forms;
using bsmd.database;
namespace CoreDeleteTool
{
public partial class Main : Form
{
private readonly SortableBindingList<MessageCore> messageCores = new SortableBindingList<MessageCore>();
public Main()
{
InitializeComponent();
}
#region overrides
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString))
{
}
else
{
MessageBox.Show("Cannot connect to database!");
}
}
#endregion
#region event handler
private void buttonDelete_Click(object sender, EventArgs e)
{
if ((this.dataGridView.SelectedRows.Count > 0) && DBManager.Instance.IsConnected)
{
if (MessageBox.Show("Delete selected item(s)?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
this.toolStripProgressBar.Maximum = this.dataGridView.SelectedRows.Count;
this.toolStripStatusLabel.Text = string.Format("Removing {0} items..", this.dataGridView.SelectedRows.Count);
int total = this.dataGridView.SelectedRows.Count;
for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
{
if (this.dataGridView.SelectedRows[i].DataBoundItem is MessageCore selectedCore)
{
DBManager.Instance.DeleteCore(selectedCore);
this.messageCores.Remove(selectedCore);
this.toolStripProgressBar.Value = i;
this.toolStripStatusLabel.Text = string.Format("Removed {0}/{1}", (i + 1), total);
Application.DoEvents();
}
}
}
}
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewColumn column in dataGridView.Columns)
column.SortMode = DataGridViewColumnSortMode.Automatic;
}
private void menuStripDelete_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.buttonDelete_Click(sender, new EventArgs());
}
private void buttonLoadOld_Click(object sender, EventArgs e)
{
this.messageCores.Clear();
foreach (MessageCore mc in DBManager.Instance.GetMessageCoresOlderThan((int)this.numericUpDown1.Value))
this.messageCores.Add(mc);
this.dataGridView.DataSource = this.messageCores;
this.toolStripStatusLabel.Text = string.Format("{0} cores found", this.messageCores.Count);
}
private void buttonLoadAll_Click(object sender, EventArgs e)
{
foreach (MessageCore mc in DBManager.Instance.GetMessageCoresByStatus(MessageCore.BSMDStatus.PREPARE))
this.messageCores.Add(mc);
this.dataGridView.DataSource = this.messageCores;
this.toolStripStatusLabel.Text = string.Format("{0} cores found", this.messageCores.Count);
}
private void buttonDeleteAll_Click(object sender, EventArgs e)
{
if (DBManager.Instance.IsConnected)
{
if (MessageBox.Show("Delete all items?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Application.UseWaitCursor = true;
int total = this.messageCores.Count;
this.toolStripProgressBar.Maximum = this.messageCores.Count;
for (int i = 0; i < this.messageCores.Count; i++)
{
MessageCore selectedCore = this.messageCores[i];
DBManager.Instance.DeleteCore(selectedCore);
this.toolStripProgressBar.Value = i;
this.toolStripStatusLabel.Text = string.Format("Removed {0}/{1}", (i + 1), total);
Application.DoEvents();
}
Application.UseWaitCursor = false;
this.messageCores.Clear();
}
}
}
#endregion
}
}