protected virtual void btnDeleteTask_Click(object sender, EventArgs e)
 {
     btnDeleteTask.Enabled = false;
     try
     {
         int?selectedKey = rgvTasks.SelectedValue as int?;
         if (selectedKey.HasValue)
         {
             int    taskId   = Convert.ToInt32(rgvTasks.SelectedRow.Cells["Id"].Value);
             string taskName = Convert.ToString(rgvTasks.SelectedRow.Cells["Name"].Value);
             if (MessageBox.Show($"Удалить задание: [{taskName}] ?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 var res = MQueryCommand.TryDeleteTask(taskId);
                 if (!res.IsComplete)
                 {
                     MessageBox.Show(res.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
                 else
                 {
                     int oldIndex = rgvTasks.SelectedRow.Index < 0 ? 0 : rgvTasks.SelectedRow.Index;
                     int index    = rgvTasks.SelectedRow.Index - 1 < 0 ? 0 : rgvTasks.SelectedRow.Index - 1;
                     rgvTasks.dataGridView.Rows[index].Selected = true;
                     Thread.Sleep(300);
                     RefreshGrid(MQueryCommand.SelectShedulerTasksGrid());
                     rgvTasks.dataGridView.Invalidate();
                 }
             }
         }
     }
     finally
     {
         btnDeleteTask.Enabled = true;
     }
 }
Exemple #2
0
        private void DeleteTask()
        {
            this.toolStripButtonAddTask.Enabled        =
                this.toolStripButtonRemoveTask.Enabled =
                    lbTasks.Enabled = false;
            var selectedTaskItem = (ShedulerTask)lbTasks.SelectedItem;

            if (selectedTaskItem == null)
            {
                MessageBox.Show("Не выбрано ни одного пункта", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show($"Удалить задачу '{selectedTaskItem.Name}'?", "Подтвердите действие", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }

            // Удаление задачи
            var result = MQueryCommand.TryDeleteTask(selectedTaskItem.Id);

            if (result.IsComplete)
            {
                this.UpdateTasksList();
                if (this.lbTasks.Items.Count > 0)
                {
                    this.lbTasks.SelectedIndex = this.lbTasks.Items.Count - 1;
                }
                MessageBox.Show($"Задача успешно удалена", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show($"Ошибка удаления задачи{Environment.NewLine}{result.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.toolStripButtonAddTask.Enabled        =
                this.toolStripButtonRemoveTask.Enabled =
                    lbTasks.Enabled = true;
        }