Example #1
0
 private void personGreed_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
 {
     DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex];
     if (itemChange)
     {
         //row was changed
         DialogResult answer = MessageBox.Show("Сохранить?", "Сохранить?", MessageBoxButtons.YesNoCancel);
         if (answer == DialogResult.Cancel)
         {
             e.Cancel = true;
             row.ErrorText = string.Empty;
             return;
         }
         else if (answer == DialogResult.Yes)
         {
             Item item = GetItemFromSelectedRow((DataGridView)sender);
             if (item == null)
             {
                 e.Cancel = true;
                 row.ErrorText = "Некоректные данные";
                 return;
             }
             if (!item.Validate())
             {
                 e.Cancel = true;
                 row.ErrorText = "Некоректные данные";
                 return;
             }
             SocketClient socket = new SocketClient();
             if (socket.SaveItem(ref item))
             {
                 row.Cells[0].Value = item.id.ToString();
                 row.ErrorText = string.Empty;
                 Item[] items = GetArrayFromSelectedGrid((DataGridView)sender);
                 List<Item> tmp = new List<Item>(items.Length + 1);
                 tmp.AddRange(items);
                 tmp.Add(item);
                 SetArrayFromSelectedGrid((DataGridView)sender, tmp.ToArray());
                 ValidateButtons();
             }
             else
             {
                 row.ErrorText = "ошибка сервера";
                 e.Cancel = true;
             }
         }
         else
         {
             int id = 0;
             Int32.TryParse(row.Cells[0].Value.ToString(), out id);
             if (id == 0)
             {
                 //new person - clear row
                 foreach (DataGridViewCell cell in row.Cells)
                 {
                     if (cell is DataGridViewComboBoxCell)
                     {
                         cell.Value = ((DataGridViewComboBoxCell)cell).Items[0];
                     }
                     else
                     {
                         cell.Value = string.Empty;
                     }
                 }
                 //это писец!!! я задолбался это решение гуглить!
                 BeginInvoke(new Action(delegate { ((DataGridView)sender).Rows.RemoveAt(e.RowIndex); }));
             }
             else
             {
                 //update from caсhe
                 Item[] items = GetArrayFromSelectedGrid(personGrid);
                 foreach (Item item in items)
                 {
                     if (item.id == id)
                     {
                         PutItemToRow(ref row, item);
                         break;
                     }
                 }
             }
             row.ErrorText = string.Empty;
         }
     }
     itemChange = e.Cancel;
 }
Example #2
0
 private void EditMark(bool isAdd)
 {
     if (subjects == null || subjects.Length == 0 ||
        persons == null || persons.Length == 0)
     {
         MessageBox.Show("Для ввода оценок нужно добавить студентов и предметы.", "Предупреждение",
             MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;
     }
     Mark mark = null;
     DataGridViewRow row = null;
     if (!isAdd)
     {
         mark = (Mark)markGrid.CurrentRow.Cells[0].Value;
         row = markGrid.CurrentRow;
     }
     MarkForm form = new MarkForm();
     form.subjects = subjects;
     form.persons = persons;
     form.mark = mark;
     form.FillData();
     if (form.ShowDialog() == DialogResult.OK)
     {
         mark = form.mark;
         Item item = (Item)mark;
         SocketClient client = new SocketClient();
         if (client.SaveItem(ref item))
         {
             List<Item> tmp = new List<Item>(marks.Length + 1);
             tmp.AddRange(marks);
             tmp.Add(item);
             marks = tmp.ToArray();
             PutItemToRow(ref row, item);
             if (isAdd)
             {
                 markGrid.Rows.Add(row);
                 ValidateButtons();
             }
         }
     }
 }