private void formOrderManagement_Load(object sender, EventArgs e)
{
try
{
LoadMenu(ultraGridCustomer);
ultraGridCustomer.DataSource = m_WebService.LoadAllCustomers();
this.ultraGridCustomer.KeyActionMappings.Add(new GridKeyActionMapping(Keys.Enter, UltraGridAction.CommitRow, UltraGridState.RowFirst, UltraGridState.Row, 0, 0));
}
catch (Exception ex)
{
MessageBox.Show("Error occurred when loading customer, the error message is: " + ex.ToString());
}
}
private void customerMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
try
{
if (e.ClickedItem.Text == "Add")
{
ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.TemplateOnBottom;
ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;
customerMenuStrip.Items["Edit"].Enabled = false;
customerMenuStrip.Items["Delete"].Enabled = false;
customerMenuStrip.Items["Add"].Visible = false;
return;
}
else
{
customerMenuStrip.Items["Edit"].Enabled = true;
customerMenuStrip.Items["Delete"].Enabled = true;
customerMenuStrip.Items["Add"].Visible = true;
}
if (e.ClickedItem.Text == "Edit")
{
ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;
ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.Yes;
customerMenuStrip.Items["Add"].Enabled = false;
customerMenuStrip.Items["Delete"].Enabled = false;
customerMenuStrip.Items["Edit"].Visible = false;
return;
}
else
{
customerMenuStrip.Items["Add"].Enabled = true;
customerMenuStrip.Items["Delete"].Enabled = true;
customerMenuStrip.Items["Edit"].Visible = true;
}
if (e.ClickedItem.Text == "Browser")
{
ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.Yes;
customerMenuStrip.Items["Add"].Enabled = true;
customerMenuStrip.Items["Edit"].Enabled = true;
customerMenuStrip.Items["Delete"].Enabled = true;
customerMenuStrip.Items["Add"].Visible = true;
customerMenuStrip.Items["Edit"].Visible = true;
customerMenuStrip.Items["Delete"].Visible = true;
}
if (e.ClickedItem.Text == "Delete")
{
if (ultraGridCustomer.Selected.Rows.Count > 0 && !string.IsNullOrWhiteSpace(ultraGridCustomer.ActiveRow.Cells["customer_id"].Value.ToString()))
{
if (MessageBox.Show("Please confirm your operation, delete " + ultraGridCustomer.ActiveRow.Cells["customer_first_name"].Value.ToString() + "?", "Attention", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
bool isDeleted;
isDeleted = m_WebService.DeleteCustomerById(Convert.ToInt32(ultraGridCustomer.ActiveRow.Cells["customer_id"].Value));
if (isDeleted)
{
ultraGridCustomer.DeleteSelectedRows(false);
ultraGridCustomer.Refresh();
MessageBox.Show("Delete successful.");
}
else
{
MessageBox.Show("Delete failed.");
}
}
}
else
{
MessageBox.Show("Data error, please restart the application.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred when item clicked, the error message is: " + ex.ToString());
}
}
private void ultraGridCustomer_MouseDown(object sender, MouseEventArgs e)
{
try
{
UltraGridRow row;
Infragistics.Win.UIElement element;
if (e.Button == MouseButtons.Right)
{
element = ultraGridCustomer.DisplayLayout.UIElement.ElementFromPoint(e.Location);
row = element.GetContext(typeof(UltraGridRow)) as UltraGridRow;
if (row != null && row.IsDataRow)
{
ultraGridCustomer.ActiveRow = row;
customerMenuStrip.Show(MousePosition.X, MousePosition.Y);
}
return;
}
}
private void LoadMenu(Control control)
{
customerMenuStrip.Items.Add("Add");
customerMenuStrip.Items.Add("Edit");
customerMenuStrip.Items.Add("Delete");
customerMenuStrip.Items.Add("Browse");
}