private void ShowContextMenu(object sender, int x_pos = -1, int y_pos = -1) { if (((DataGridView)sender).Rows.Count == 0) { return; } int row_index = ((DataGridView)sender).CurrentCell.RowIndex; EventCalendar event_calendar = null; var ev = ((DataGridView)sender).Rows[row_index].Cells[0].Value; if (ev != null) { event_calendar = ev as EventCalendar; } ContextMenu cm = new ContextMenu(); MenuItem mnu_add = new MenuItem(); mnu_add.Text = "เพิ่ม"; mnu_add.Click += delegate { DateEventWindow date_event = new DateEventWindow(this, true); date_event.ShowDialog(); }; cm.MenuItems.Add(mnu_add); MenuItem mnu_edit = new MenuItem(); mnu_edit.Text = "แก้ไข"; mnu_edit.Enabled = event_calendar != null ? true : false; mnu_edit.Click += delegate { DateEventWindow date_event = new DateEventWindow(this, true, event_calendar); date_event.ShowDialog(); }; cm.MenuItems.Add(mnu_edit); MenuItem mnu_copy = new MenuItem(); mnu_copy.Text = "คัดลอกรายการนี้ไปยังวันที่ ..."; mnu_copy.Enabled = event_calendar != null ? true : false; mnu_copy.Click += delegate { DateSelectorDialog ds = new DateSelectorDialog(this.date.Value); if (ds.ShowDialog() == DialogResult.OK) { this.DoCopy(ds.selected_date, (EventCalendar)this.dgv.Rows[row_index].Cells[0].Value); } }; cm.MenuItems.Add(mnu_copy); MenuItem mnu_delete = new MenuItem(); mnu_delete.Text = "ลบ"; mnu_delete.Enabled = event_calendar != null ? true : false; mnu_delete.Click += delegate { if (MessageAlert.Show(StringResource.CONFIRM_DELETE, "", MessageAlertButtons.OK_CANCEL, MessageAlertIcons.QUESTION) == DialogResult.OK) { bool delete_success = false; string err_msg = ""; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { CRUDResult delete = ApiActions.DELETE(PreferenceForm.API_MAIN_URL() + "eventcalendar/delete&id=" + ((EventCalendar)this.dgv.Rows[row_index].Cells[0].Value).id.ToString()); ServerResult sr = JsonConvert.DeserializeObject <ServerResult>(delete.data); if (sr.result == ServerResult.SERVER_RESULT_SUCCESS) { delete_success = true; } else { delete_success = false; err_msg = sr.message; } }; worker.RunWorkerCompleted += delegate { if (delete_success) { this.RefreshData(); this.RefreshView(); } else { this.RefreshData(); this.RefreshView(); MessageAlert.Show(err_msg, "Error", MessageAlertButtons.OK, MessageAlertIcons.ERROR); } }; worker.RunWorkerAsync(); } }; cm.MenuItems.Add(mnu_delete); if (x_pos < 0 || y_pos < 0) { x_pos = ((DataGridView)sender).GetCellDisplayRectangle(2, row_index, true).X; y_pos = ((DataGridView)sender).GetCellDisplayRectangle(2, row_index, true).Y; } cm.Show((DataGridView)sender, new Point(x_pos, y_pos)); }
private void BindingControlEvent() { #region Nulling current dgv current cell when it's leave this.dgv.Leave += delegate { this.dgv.CurrentCell = null; }; #endregion Nulling current dgv current cell when it's leave #region Show dgv row context menu this.dgv.MouseClick += delegate(object sender, MouseEventArgs e) { if (!this.read_only) { if (e.Button == MouseButtons.Right) { this.dgv.Focus(); int row_index = this.dgv.HitTest(e.X, e.Y).RowIndex; if (row_index > -1 && this.G.loged_in_user_level >= GlobalVar.USER_LEVEL_SUPERVISOR && this.dgv.Rows[row_index].Tag is EventCalendar) { this.dgv.Rows[row_index].Cells[1].Selected = true; ContextMenu m = new ContextMenu(); MenuItem m_edit = new MenuItem("แก้ไข"); m_edit.Click += delegate { //DateEventWindow wind = new DateEventWindow(this, true, (EventCalendar)this.dgv.Rows[this.dgv.CurrentCell.RowIndex].Tag); //wind.ShowDialog(); }; m.MenuItems.Add(m_edit); MenuItem m_copy = new MenuItem("คัดลอกไปยังวันที่ ..."); m_copy.Click += delegate { DateSelectorDialog ds = new DateSelectorDialog(this.Date); if (ds.ShowDialog() == DialogResult.OK) { this.DoCopy(ds.selected_date, (EventCalendar)this.dgv.Rows[row_index].Tag); } }; m.MenuItems.Add(m_copy); MenuItem m_delete = new MenuItem("ลบ"); m_delete.Click += delegate { if (MessageAlert.Show(StringResource.CONFIRM_DELETE, "", MessageAlertButtons.OK_CANCEL, MessageAlertIcons.QUESTION) == DialogResult.OK) { this.Ready = false; bool delete_success = false; string err_msg = ""; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { CRUDResult delete = ApiActions.DELETE(PreferenceForm.API_MAIN_URL() + "eventcalendar/delete&id=" + ((EventCalendar)this.dgv.Rows[row_index].Tag).id.ToString()); ServerResult sr = JsonConvert.DeserializeObject <ServerResult>(delete.data); if (sr.result == ServerResult.SERVER_RESULT_SUCCESS) { delete_success = true; } else { delete_success = false; err_msg = sr.message; } }; worker.RunWorkerCompleted += delegate { if (delete_success) { this.RefreshData(); this.RefreshView(); this.Ready = true; } else { this.RefreshData(); this.RefreshView(); MessageAlert.Show(err_msg, "Error", MessageAlertButtons.OK, MessageAlertIcons.ERROR); } }; worker.RunWorkerAsync(); } }; m.MenuItems.Add(m_delete); m.Show(this.dgv, new Point(e.X, e.Y)); } } } }; #endregion Show dgv row context menu }