Exemple #1
0
        private void dgvResources_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (e.Row.HaveSource())
            {
                try
                {
                    using (var ctx = new OpenDataContext())
                    {
                        var res = ctx.resources.Find(((resource)e.Row.Cells[MyHelper.strSource].Value).resources_id);
                        //ctx.resources.Attach(res);

                        bool cancel = res.buildings_resources_consume.Count > 0;

                        if (cancel)
                        {
                            MessageBox.Show("Невозможно удалить ресурс, который используется!");
                            e.Cancel = true;
                            return;
                        }

                        ctx.resources.Remove(res);
                        ctx.SaveChanges();
                        cbcResorcesId.Remove(res.resources_id);
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }
Exemple #2
0
        private void InitializeDGVResources()
        {
            dgvResources.CancelEdit();
            dgvResources.Rows.Clear();
            dgvResources.Columns.Clear();
            dgvResources.DefaultCellStyle.NullValue = null;
            cbcResorcesId.InitializeDataTableResources();

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

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

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

            try
            {
                using (var ctx = new OpenDataContext())
                {
                    foreach (var res in ctx.resources)
                    {
                        dgvResources.Rows.Add(res.resources_name, res.resources_id, res);
                        cbcResorcesId.Add(res.resources_id, res.resources_name);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Exemple #3
0
 private void dgvRConsume_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
 {
     if (e.Row.HaveSource())
     {
         try
         {
             using (var ctx = new OpenDataContext())
             {
                 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);
         }
     }
 }
Exemple #4
0
        private void InitializeDGVRConsume()
        {
            dgvRConsume.CancelEdit();
            dgvRConsume.Rows.Clear();
            dgvRConsume.Columns.Clear();
            dgvRConsume.DefaultCellStyle.NullValue = null;
            cbcBuldingsId.InitializeDataTableBuildings();

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

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

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

            try
            {
                using (var ctx = new OpenDataContext())
                {
                    foreach (var build in ctx.buildings)
                    {
                        cbcBuldingsId.Add(build.building_id, build.building_name, build.outpost_id);
                    }

                    foreach (var brc in ctx.buildings_resources_consume)
                    {
                        dgvRConsume.Rows.Add(brc.building_id, brc.resources_id, brc.consume_speed, brc);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Exemple #5
0
        private void dgvResources_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            var row = dgvResources.Rows[e.RowIndex];

            if (row.IsNewRow || !dgvResources.IsCurrentRowDirty)
            {
                return;
            }
            //var cell = dgvOutposts[e.ColumnIndex, e.RowIndex];
            //var cellFormatedValue = cell.FormattedValue.ToString().RmvExtrSpaces();

            // Проверка можно ли фиксировать строку
            var cellWithPotentialError = dgvResources[MyHelper.strResourceName, e.RowIndex];

            if (cellWithPotentialError.FormattedValue.ToString().RmvExtrSpaces() == "")
            {
                cellWithPotentialError.ErrorText = MyHelper.strEmptyCell;
                row.ErrorText = MyHelper.strBadRow;
                return;
            }
            else
            {
                cellWithPotentialError.ErrorText = "";
                row.ErrorText = "";
            }

            try
            {
                using (var ctx = new OpenDataContext())
                {
                    if (row.HaveSource())
                    {
                        var new_res = (resource)row.Cells[MyHelper.strSource].Value;
                        ctx.resources.Attach(new_res);

                        string new_resources_name = (string)row.Cells[MyHelper.strResourceName].Value;

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

                        new_res.resources_name = new_resources_name;

                        ctx.SaveChanges();
                        cbcResorcesId.Change(new_res.resources_id, new_res.resources_name);
                    }
                    else
                    {
                        string new_resources_name = (string)row.Cells[MyHelper.strResourceName].Value;

                        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;
                        cbcResorcesId.Add(new_res.resources_id, new_res.resources_name);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Exemple #6
0
        private void dgvRConsume_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            var row = dgvRConsume.Rows[e.RowIndex];

            if (row.IsNewRow || !dgvRConsume.IsCurrentRowDirty)
            {
                return;
            }
            //var cell = dgvOutposts[e.ColumnIndex, e.RowIndex];
            //var cellFormatedValue = cell.FormattedValue.ToString().RmvExtrSpaces();

            // Проверка можно ли фиксировать строку
            var cellsWithPotentialErrors = new List <DataGridViewCell> {
                row.Cells[MyHelper.strBuildingId],
                row.Cells[MyHelper.strResourceId],
                row.Cells[MyHelper.strConsumeSpeed],
            };

            foreach (var cellWithPotentialError in cellsWithPotentialErrors)
            {
                if (cellWithPotentialError.FormattedValue.ToString().RmvExtrSpaces() == "")
                {
                    cellWithPotentialError.ErrorText = MyHelper.strEmptyCell;
                    row.ErrorText = MyHelper.strBadRow;
                }
                else
                {
                    cellWithPotentialError.ErrorText = "";
                }
            }
            if (cellsWithPotentialErrors.FirstOrDefault(cellWithPotentialError => cellWithPotentialError.ErrorText.Length > 0) == null)
            {
                row.ErrorText = "";
            }
            else
            {
                return;
            }

            try
            {
                using (var ctx = new OpenDataContext())
                {
                    if (row.HaveSource())
                    {
                        var new_brc = (buildings_resources_consume)row.Cells[MyHelper.strSource].Value;
                        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 = $"Для данного здания потребляемый ресурс уже существует!";
                            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();
                    }
                    else
                    {
                        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.building_id == new_building_id &&
                                                                                          brc.resources_id == new_resource_id) != null)
                        {
                            string eo = $"Для данного здания потребляемый ресурс уже существует!";
                            MessageBox.Show(eo);
                            row.ErrorText = MyHelper.strBadRow + " " + eo;
                            return;
                        }

                        var 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);

                        ctx.SaveChanges();

                        row.Cells[MyHelper.strSource].Value = new_brc;
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }