コード例 #1
0
        protected override void Update(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    outpost o = ctx.outposts.Find((int)row.Cells[MyHelper.strOutpostId].Value);

                    string new_outpost_name           = row.Cells[MyHelper.strOutpostName].Value.ToString().RmvExtrSpaces();
                    int    new_outpost_economic_value = (int)row.Cells[MyHelper.strOutpostEconomicValue].Value;
                    int    new_outpost_coordinate_x   = (int)row.Cells[MyHelper.strOutpostCoordinateX].Value;
                    int    new_outpost_coordinate_y   = (int)row.Cells[MyHelper.strOutpostCoordinateY].Value;
                    int    new_outpost_coordinate_z   = (int)row.Cells[MyHelper.strOutpostCoordinateZ].Value;

                    if (ctx.outposts.AsEnumerable().FirstOrDefault(outpst =>
                                                                   outpst.outpost_id != o.outpost_id &&
                                                                   outpst.outpost_name == new_outpost_name &&
                                                                   outpst.outpost_coordinate_x == new_outpost_coordinate_x &&
                                                                   outpst.outpost_coordinate_y == new_outpost_coordinate_y &&
                                                                   outpst.outpost_coordinate_z == new_outpost_coordinate_z) != null)
                    {
                        string eo = $"Форпост {new_outpost_name} - {new_outpost_coordinate_x};{new_outpost_coordinate_y};{new_outpost_coordinate_z} идентичен существующему!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    o.outpost_name           = new_outpost_name;
                    o.outpost_economic_value = new_outpost_economic_value;
                    o.outpost_coordinate_x   = new_outpost_coordinate_x;
                    o.outpost_coordinate_y   = new_outpost_coordinate_y;
                    o.outpost_coordinate_z   = new_outpost_coordinate_z;

                    //ctx.Entry(o).State = EntityState.Modified;
                    ctx.SaveChanges();
                    _outpostDataTableHandler.Change(o.outpost_id, o.outpost_name, o.outpost_coordinate_x, o.outpost_coordinate_y, o.outpost_coordinate_z);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
コード例 #2
0
        public override void UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (e.Row.HaveSource())
            {
                try
                {
                    using (var ctx = new OutpostDataContext())
                    {
                        var o = ctx.outposts.Find(e.Row.Cells[MyHelper.strOutpostId].Value);

                        if (o.buildings.Count > 0 ||
                            o.storage_resources.Count > 0)
                        {
                            MessageBox.Show($"Вы не можете удалить Форпост {o.outpost_name} - {o.outpost_coordinate_x};{o.outpost_coordinate_y};{o.outpost_coordinate_z}, так как он используется");
                            e.Cancel = true;
                            return;
                        }

                        if (MessageBox.Show($"Вы уверены, что хотите удалить {o.outpost_name} - {o.outpost_coordinate_x};{o.outpost_coordinate_y};{o.outpost_coordinate_z}?", "Предупреждение!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            ctx.outposts.Remove(o);
                            ctx.SaveChanges();
                            _outpostDataTableHandler.Remove(o.outpost_id);
                        }
                        else
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }