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); } }
//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; } }
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; } } }
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); } }
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); } }
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); } } }