예제 #1
0
        //protected void HideColumns()
        //{
        //    MakeThisColumnVisible(new string[] {
        //            "resources_name"
        //        });
        //}

        protected override void Insert(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    string new_resources_name = ((string)row.Cells[MyHelper.strResourceName].Value).RmvExtrSpaces();

                    if (ctx.resources.AsEnumerable().FirstOrDefault(res => res.resources_name.ToLower() == new_resources_name.ToLower()) != null)
                    {
                        string eo = $"Ресурс {new_resources_name} уже существует!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    var new_res = new resource();
                    new_res.resources_name = new_resources_name;
                    ctx.resources.Add(new_res);
                    ctx.SaveChanges();
                    row.Cells[MyHelper.strSource].Value     = new_res;
                    row.Cells[MyHelper.strResourceId].Value = new_res.resources_id;
                    _resourcesDataTableHandler.Add(new_res.resources_id, new_res.resources_name);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                row.ErrorText = MyHelper.strError + err.Message;
            }
        }
예제 #2
0
        public override void Initialize()
        {
            _dgv.CancelEdit();
            _dgv.Rows.Clear();
            _dgv.Columns.Clear();

            _dgv.Columns.Add(_cbcOutposts);
            _dgv.Columns.Add(_cbcResources);
            _dgv.Columns.Add(MyHelper.strCount, "Количество");
            _dgv.Columns.Add(MyHelper.strAccumulationSpeed, "Скорость накопления");
            _dgv.Columns.Add(MyHelper.strSource, "src");

            foreach (DataGridViewColumn column in _dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            _dgv.Columns[MyHelper.strOutpostId].ValueType         = typeof(int);
            _dgv.Columns[MyHelper.strResourceId].ValueType        = typeof(int);
            _dgv.Columns[MyHelper.strCount].ValueType             = typeof(int);
            _dgv.Columns[MyHelper.strAccumulationSpeed].ValueType = typeof(int);
            _dgv.Columns[MyHelper.strSource].ValueType            = typeof(storage_resources);

            _dgv.Columns[MyHelper.strSource].Visible = false;

            using (var ctx = new OutpostDataContext())
            {
                foreach (var sr in ctx.storage_resources)
                {
                    _dgv.Rows.Add(sr.outpost_id, sr.resources_id, sr.count, sr.accumulation_speed, sr);
                }
            }
        }
예제 #3
0
        public override void UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (e.Row.HaveSource())
            {
                try
                {
                    using (var ctx = new OutpostDataContext())
                    {
                        var res = ctx.resources.Find(((resource)e.Row.Cells[MyHelper.strSource].Value).resources_id);

                        if (res.buildings_resources_consume.Count > 0 ||
                            res.buildings_resources_produce.Count > 0 ||
                            res.storage_resources.Count > 0 ||
                            res.machines_resources_consume.Count > 0)
                        {
                            MessageBox.Show($"Вы не можете удалить ресурс {res.resources_name}, так как он используется");
                            e.Cancel = true;
                            return;
                        }

                        ctx.resources.Remove(res);
                        ctx.SaveChanges();
                        _resourcesDataTableHandler.Remove(res.resources_id);
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    e.Row.ErrorText = MyHelper.strError + err.Message;
                    e.Cancel        = true;
                }
            }
        }
예제 #4
0
        protected override void Update(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    var new_building = ctx.buildings.Find((int)row.Cells[MyHelper.strBuildingId].Value);

                    string new_building_name = ((string)row.Cells[MyHelper.strBuildingName].Value).RmvExtrSpaces();
                    var    new_outpost_id    = row.Cells[MyHelper.strOutpostId].Value;

                    if (ctx.buildings.AsEnumerable().FirstOrDefault(b => b.building_id != new_building.building_id && b.building_name.ToLower() == new_building_name.ToLower()) != null)
                    {
                        string eo = $"Здание {new_building_name} уже существует!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    new_building.building_name = new_building_name;
                    new_building.outpost_id    = new_outpost_id == null || new_outpost_id == DBNull.Value ? new int?() : new int?((int)new_outpost_id);//(int?)new_outpost_id;

                    ctx.SaveChanges();
                    _buildingsDataTableHandler.Change(new_building.building_id, new_building.building_name);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
예제 #5
0
        protected override void Update(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    var new_sr = (storage_resources)row.Cells[MyHelper.strSource].Value;
                    ctx.storage_resources.Attach(new_sr);

                    int new_outpost_id         = (int)row.Cells[MyHelper.strOutpostId].Value;
                    int new_resource_id        = (int)row.Cells[MyHelper.strResourceId].Value;
                    int new_count              = (int)row.Cells[MyHelper.strCount].Value;
                    int new_accumulation_speed = (int)row.Cells[MyHelper.strAccumulationSpeed].Value;

                    if (ctx.storage_resources.AsEnumerable().FirstOrDefault(sr =>
                                                                            sr != new_sr &&
                                                                            sr.outpost_id == new_outpost_id &&
                                                                            sr.resources_id == new_resource_id) != null)
                    {
                        string eo = $"Для форпоста {row.Cells[MyHelper.strOutpostId].FormattedValue} " +
                                    $"ресурс {row.Cells[MyHelper.strResourceId].FormattedValue} " +
                                    $"уже существует! Измените или удалите текущую строку!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    if (new_sr.resources_id != new_resource_id || new_sr.outpost_id != new_outpost_id)
                    {
                        ctx.storage_resources.Remove(new_sr);
                        ctx.SaveChanges();
                        new_sr = new storage_resources();

                        new_sr.outpost_id         = new_outpost_id;
                        new_sr.resources_id       = new_resource_id;
                        new_sr.count              = new_count;
                        new_sr.accumulation_speed = new_accumulation_speed;
                        ctx.storage_resources.Add(new_sr);
                    }
                    else
                    {
                        new_sr.count = new_count;
                        new_sr.accumulation_speed = new_accumulation_speed;
                    }

                    ctx.SaveChanges();
                    row.Cells[MyHelper.strSource].Value = new_sr;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        protected override void Update(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    var new_brc = (buildings_resources_consume)row.Cells[MyHelper.strSource].Value;
                    //var tmp = (buildings_resources_consume)row.Cells[MyHelper.strSource].Value;
                    //var new_brc = ctx.buildings_resources_consume.Find(tmp.building_id, tmp.resources_id);
                    ctx.buildings_resources_consume.Attach(new_brc);

                    int new_building_id   = (int)row.Cells[MyHelper.strBuildingId].Value;
                    int new_resource_id   = (int)row.Cells[MyHelper.strResourceId].Value;
                    int new_consume_speed = (int)row.Cells[MyHelper.strConsumeSpeed].Value;

                    if (ctx.buildings_resources_consume.AsEnumerable().FirstOrDefault(brc =>
                                                                                      brc != new_brc &&
                                                                                      brc.building_id == new_building_id &&
                                                                                      brc.resources_id == new_resource_id) != null)
                    {
                        string eo = $"Для здания {row.Cells["building_id"].FormattedValue} " +
                                    $"потребляемый ресурс {row.Cells["resources_id"].FormattedValue} " +
                                    $"уже существует! Измените или удалите текущую строку!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    if (new_brc.resources_id != new_resource_id || new_brc.building_id != new_building_id)
                    {
                        ctx.buildings_resources_consume.Remove(new_brc);
                        ctx.SaveChanges();
                        new_brc = new buildings_resources_consume();

                        new_brc.resources_id  = new_resource_id;
                        new_brc.building_id   = new_building_id;
                        new_brc.consume_speed = new_consume_speed;
                        ctx.buildings_resources_consume.Add(new_brc);
                    }
                    else
                    {
                        new_brc.consume_speed = new_consume_speed;
                    }

                    ctx.SaveChanges();
                    row.Cells[MyHelper.strSource].Value = new_brc;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
예제 #7
0
        public override void Initialize()
        {
            _dgv.CancelEdit();
            _dgv.Rows.Clear();
            _dgv.Columns.Clear();
            _buildingsDataTableHandler.InitializeDataTableBuildings();

            _dgv.Columns.Add(MyHelper.strBuildingName, "Название здания");
            //_dgv.Columns.Add(MyHelper.strOutpostId, "Форпост");
            _dgv.Columns.Add(_cbcOutpost);
            _dgv.Columns.Add(MyHelper.strBuildingId, "id");
            _dgv.Columns.Add(MyHelper.strSource, "src");

            foreach (DataGridViewColumn column in _dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            _dgv.Columns[MyHelper.strBuildingName].ValueType = typeof(string);
            _dgv.Columns[MyHelper.strOutpostId].ValueType    = typeof(int);
            _dgv.Columns[MyHelper.strBuildingId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strSource].ValueType       = typeof(building);

            _dgv.Columns[MyHelper.strBuildingId].Visible = false;
            _dgv.Columns[MyHelper.strSource].Visible     = false;

            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    foreach (var build in ctx.buildings)
                    {
                        _dgv.Rows.Add(build.building_name, build.outpost_id, build.building_id, build);
                        _buildingsDataTableHandler.Add(build.building_id, build.building_name);
                    }
                }
                _dgv.Columns[MyHelper.strOutpostId].ContextMenuStrip = contextMenuStrip;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
 public override void UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
 {
     if (e.Row.HaveSource())
     {
         try
         {
             using (var ctx = new OutpostDataContext())
             {
                 var brc = (buildings_resources_consume)e.Row.Cells[MyHelper.strSource].Value;
                 ctx.buildings_resources_consume.Attach(brc);
                 ctx.buildings_resources_consume.Remove(brc);
                 ctx.SaveChanges();
             }
         }
         catch (Exception err)
         {
             MessageBox.Show(err.Message);
         }
     }
 }
        protected override void Insert(DataGridViewRow row)
        {
            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    int new_building_id   = (int)row.Cells[MyHelper.strBuildingId].Value;
                    int new_resource_id   = (int)row.Cells[MyHelper.strResourceId].Value;
                    int new_produce_speed = (int)row.Cells[MyHelper.strProduceSpeed].Value;

                    if (ctx.buildings_resources_produce.AsEnumerable().FirstOrDefault(brp =>
                                                                                      brp.building_id == new_building_id &&
                                                                                      brp.resources_id == new_resource_id) != null)
                    {
                        string eo = $"Для здания {row.Cells["building_id"].FormattedValue} " +
                                    $"производимый ресурс {row.Cells["resources_id"].FormattedValue} " +
                                    $"уже существует! Измените или удалите текущую строку!";
                        MessageBox.Show(eo);
                        row.ErrorText = MyHelper.strBadRow + " " + eo;
                        return;
                    }

                    var new_brp = new buildings_resources_produce();
                    new_brp.resources_id  = new_resource_id;
                    new_brp.building_id   = new_building_id;
                    new_brp.produce_speed = new_produce_speed;

                    ctx.buildings_resources_produce.Add(new_brp);

                    ctx.SaveChanges();

                    row.Cells[MyHelper.strSource].Value = new_brp;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
예제 #10
0
        public override void Initialize()
        {
            _dgv.CancelEdit();
            _dgv.Rows.Clear();
            _dgv.Columns.Clear();
            _resourcesDataTableHandler.InitializeDataTableResources();

            _dgv.Columns.Add(MyHelper.strResourceName, "Название ресурса");
            _dgv.Columns.Add(MyHelper.strResourceId, "id");
            _dgv.Columns.Add(MyHelper.strSource, "");

            foreach (DataGridViewColumn column in _dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            _dgv.Columns[MyHelper.strResourceName].ValueType = typeof(string);
            _dgv.Columns[MyHelper.strResourceId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strSource].ValueType       = typeof(resource);

            _dgv.Columns[MyHelper.strResourceId].Visible = false;
            _dgv.Columns[MyHelper.strSource].Visible     = false;

            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    foreach (var res in ctx.resources)
                    {
                        _dgv.Rows.Add(res.resources_name, res.resources_id, res);
                        _resourcesDataTableHandler.Add(res.resources_id, res.resources_name);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        public override void Initialize()
        {
            _dgv.CancelEdit();
            _dgv.Rows.Clear();
            _dgv.Columns.Clear();

            _dgv.Columns.Add(_cbcBuilsings);
            _dgv.Columns.Add(_cbcResources);
            _dgv.Columns.Add(MyHelper.strConsumeSpeed, "Скорость потребления");
            _dgv.Columns.Add(MyHelper.strSource, "");

            foreach (DataGridViewColumn column in _dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            _dgv.Columns[MyHelper.strBuildingId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strResourceId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strConsumeSpeed].ValueType = typeof(int);
            _dgv.Columns[MyHelper.strSource].ValueType       = typeof(buildings_resources_consume);

            _dgv.Columns[MyHelper.strSource].Visible = false;

            try
            {
                using (var ctx = new OutpostDataContext())
                {
                    foreach (var brc in ctx.buildings_resources_consume)
                    {
                        _dgv.Rows.Add(brc.building_id, brc.resources_id, brc.consume_speed, brc);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
예제 #12
0
        public override void UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (e.Row.HaveSource())
            {
                try
                {
                    using (var ctx = new OutpostDataContext())
                    {
                        var building = ctx.buildings.Find(((building)e.Row.Cells[MyHelper.strSource].Value).building_id);

                        if (building.buildings_resources_consume.Count > 0 ||
                            building.buildings_resources_produce.Count > 0)
                        {
                            MessageBox.Show($"Вы не можете удалить здание {building.building_name}, так как оно используется в других таблицах");
                            e.Cancel = true;
                            return;
                        }

                        if (MessageBox.Show($"Вы уверены, что хотите удалить информацию о здании {building.building_name}?", "Предупреждение!", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            ctx.buildings.Attach(building);
                            ctx.buildings.Remove(building);
                            ctx.SaveChanges();
                            _buildingsDataTableHandler.Remove(building.building_id);
                        }
                        else
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }
예제 #13
0
        public override void Initialize()
        {
            _dgv.CancelEdit();
            _dgv.Rows.Clear();
            _dgv.Columns.Clear();

            _dgv.Columns.Add(_cbcBuilsings);
            _dgv.Columns.Add(_cbcResources);
            _dgv.Columns.Add(MyHelper.strConsumeSpeed, "Скорость потребления");
            _dgv.Columns.Add(MyHelper.strProduceSpeed, "Скорость производства");

            foreach (DataGridViewColumn column in _dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            _dgv.Columns[MyHelper.strBuildingId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strResourceId].ValueType   = typeof(int);
            _dgv.Columns[MyHelper.strConsumeSpeed].ValueType = typeof(int);
            _dgv.Columns[MyHelper.strProduceSpeed].ValueType = typeof(int);

            using (var ctx = new OutpostDataContext())
            {
                var query = from building in ctx.buildings
                            from resource in ctx.resources

                            join brc in ctx.buildings_resources_consume
                            on new { building.building_id, resource.resources_id } equals new { brc.building_id, brc.resources_id } into leftJoin1
                from brc1 in leftJoin1.DefaultIfEmpty()

                join brp in ctx.buildings_resources_produce
                    on new { building.building_id, resource.resources_id } equals new { brp.building_id, brp.resources_id } into leftJoin2
                from brp1 in leftJoin2.DefaultIfEmpty()

                select new
                {
                    building_id   = building.building_id,
                    resources_id  = resource.resources_id,
                    consume_speed = (int?)brc1.consume_speed,
                    produce_speed = (int?)brp1.produce_speed
                };
                foreach (var item in query)
                {
                    _dgv.Rows.Add(item.building_id, item.resources_id, item.consume_speed.HasValue ? item.consume_speed : 0, item.produce_speed.HasValue ? item.produce_speed : 0);
                }
            }


            //return;
            //var connectionString = ConfigurationManager.ConnectionStrings["OutpostDataContext"].ConnectionString;
            //using (var c = new NpgsqlConnection(connectionString))
            //{
            //    c.Open();
            //    var comm = new NpgsqlCommand()
            //    {
            //        Connection = c,
            //        CommandText = @"
            //    SELECT  buildings.building_id,
            //            resources.resources_id,
            //            COALESCE(buildings_resources_consume.consume_speed, 0) AS consume_speed,
            //            COALESCE(buildings_resources_produce.produce_speed, 0) AS produce_speed
            //    FROM buildings
            //    CROSS JOIN resources
            //    FULL JOIN buildings_resources_produce ON buildings.building_id = buildings_resources_produce.building_id AND
            //                                            resources.resources_id = buildings_resources_produce.resources_id
            //    FULL JOIN buildings_resources_consume ON buildings.building_id = buildings_resources_consume.building_id AND
            //                                                                resources.resources_id = buildings_resources_consume.resources_id;"
            //    };
            //    var r = comm.ExecuteReader();

            //    while (r.Read())
            //        _dgv.Rows.Add(r["building_id"], r["resources_id"], r["consume_speed"], r["produce_speed"], 1);

            //}
        }