// Save changes in grid view back to database private void btnSave_Click(object sender, EventArgs e) { int test; // Used to test for numerical values // Games tab is selected if (tabMain.SelectedTab.Name == "tpGames") { foreach (DataGridViewRow row in gridMain.Rows) { // Skip this row if it's entirely empty if (CheckEmptyRow(row)) continue; // Insert row into database if new row // GID will not be set if row was added by user if (row.Cells["colGID"].Value == null || mainChangedRows.Contains(row.Cells["colGID"].RowIndex)) { // Get data from cells, while checking for valid input DBHandler.GameInfo newGame = new DBHandler.GameInfo(); if (row.Cells["colConsole"].Value != null) newGame.name = row.Cells["colName"].Value.ToString(); else newGame.name = "[UNTITLED]"; if (row.Cells["colPublisher"].Value != null) newGame.publisher = row.Cells["colPublisher"].Value.ToString(); else newGame.publisher = ""; if (row.Cells["colGenre"].Value != null) newGame.genre = row.Cells["colGenre"].Value.ToString(); else newGame.genre = ""; if (row.Cells["colConsole"].Value != null) newGame.consoleId = db.GetConsoleID(row.Cells["colConsole"].Value.ToString()); else newGame.consoleId = 1; if (row.Cells["colBoxed"].Value != null) newGame.boxed = Convert.ToBoolean(row.Cells["colBoxed"].Value); else newGame.boxed = false; if (row.Cells["colContainer"].Value != null) newGame.containerId = db.GetContainerID(row.Cells["colContainer"].Value.ToString()); else newGame.containerId = 1; // Insert it if (row.Cells["colGID"].Value == null) db.InsertGame(newGame); // Update it else { // Make sure GID is valid if (row.Cells["colGID"].Value != null && Int32.TryParse(row.Cells["colGID"].Value.ToString(), out test)) { newGame.gid = Convert.ToInt32(row.Cells["colGID"].Value); db.UpdateGame(newGame); } } } } } // Consoles tab is selected // Performs similarly to the above, so comments are lax else if (tabMain.SelectedTab.Name == "tpConsoles") { foreach (DataGridViewRow row in gridConsoles.Rows) { if (CheckEmptyRow(row)) continue; if (row.Cells["colCID"].Value == null || consolesChangedRows.Contains(row.Cells["colCID"].RowIndex)) { DBHandler.ConsoleInfo newConsole = new DBHandler.ConsoleInfo(); if (row.Cells["colConsoleName"].Value != null) newConsole.name = row.Cells["colConsoleName"].Value.ToString(); else newConsole.name = "[UNTITLED]"; if (row.Cells["colManufacturer"].Value != null) newConsole.manufacturer = row.Cells["colManufacturer"].Value.ToString(); else newConsole.manufacturer = ""; if (row.Cells["colConsoleContainer"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) newConsole.containerId = Convert.ToInt32(row.Cells["colConsoleContainer"].Value); else newConsole.containerId = 0; if (row.Cells["colSwitchbox"].Value != null) newConsole.switchBoxId = db.GetSwitchboxID(row.Cells["colSwitchbox"].Value.ToString()); else newConsole.switchBoxId = 0; if (row.Cells["colSwitchboxNo"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) newConsole.switchBoxNo = Convert.ToInt32(row.Cells["colSwitchboxNo"].Value); else newConsole.switchBoxNo = 0; if (row.Cells["colCID"].Value == null) db.InsertConsole(newConsole); else { if (row.Cells["colCID"].Value != null && Int32.TryParse(row.Cells["colCID"].Value.ToString(), out test)) { newConsole.cid = Convert.ToInt32(row.Cells["colCID"].Value); db.UpdateConsole(newConsole); } } } } } // Containers tab is selected else if (tabMain.SelectedTab.Name == "tpContainers") { foreach (DataGridViewRow row in gridContainers.Rows) { if (CheckEmptyRow(row)) continue; if (row.Cells["colConID"].Value == null || containersChangedRows.Contains(row.Cells["colConID"].RowIndex)) { DBHandler.ContainerInfo newContainer = new DBHandler.ContainerInfo(); if (row.Cells["colContainerName"].Value != null) newContainer.name = row.Cells["colContainerName"].Value.ToString(); else newContainer.name = "[UNTITLED]"; if (row.Cells["colConID"].Value == null) db.InsertContainer(newContainer); else { if (row.Cells["colConID"].Value != null && Int32.TryParse(row.Cells["colConID"].Value.ToString(), out test)) { newContainer.con_id = Convert.ToInt32(row.Cells["colConID"].Value); db.UpdateContainer(newContainer); } } } } } // Switchbox tab is selected else if (tabMain.SelectedTab.Name == "tpSwitchboxes") { foreach (DataGridViewRow row in gridSwitchboxes.Rows) { if (CheckEmptyRow(row)) continue; if (row.Cells["colSID"].Value == null || switchboxesChangedRows.Contains(row.Cells["colSID"].RowIndex)) { DBHandler.SwitchboxInfo newSwitchbox = new DBHandler.SwitchboxInfo(); if (row.Cells["colSwitchboxName"].Value != null) newSwitchbox.name = row.Cells["colSwitchboxName"].Value.ToString(); else newSwitchbox.name = "[UNTITLED]"; if (row.Cells["colNumSwitches"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) newSwitchbox.numSwitches = Convert.ToInt32(row.Cells["colNumSwitches"].Value); else newSwitchbox.name = "[UNTITLED]"; if (row.Cells["colSID"].Value == null) db.InsertSwitchbox(newSwitchbox); else { if (row.Cells["colSID"].Value != null && Int32.TryParse(row.Cells["colSID"].Value.ToString(), out test)) { newSwitchbox.sid = Convert.ToInt32(row.Cells["colSID"].Value); db.UpdateSwitchbox(newSwitchbox); } } } } } RefreshLists(); }
// Save changes in grid view back to database private void btnSave_Click(object sender, EventArgs e) { int test; // Used to test for numerical values // Games tab is selected if (tabMain.SelectedTab.Name == "tpGames") { foreach (DataGridViewRow row in gridMain.Rows) { // Skip this row if it's entirely empty if (CheckEmptyRow(row)) { continue; } // Insert row into database if new row // GID will not be set if row was added by user if (row.Cells["colGID"].Value == null || mainChangedRows.Contains(row.Cells["colGID"].RowIndex)) { // Get data from cells, while checking for valid input DBHandler.GameInfo newGame = new DBHandler.GameInfo(); if (row.Cells["colConsole"].Value != null) { newGame.name = row.Cells["colName"].Value.ToString(); } else { newGame.name = "[UNTITLED]"; } if (row.Cells["colPublisher"].Value != null) { newGame.publisher = row.Cells["colPublisher"].Value.ToString(); } else { newGame.publisher = ""; } if (row.Cells["colGenre"].Value != null) { newGame.genre = row.Cells["colGenre"].Value.ToString(); } else { newGame.genre = ""; } if (row.Cells["colConsole"].Value != null) { newGame.consoleId = db.GetConsoleID(row.Cells["colConsole"].Value.ToString()); } else { newGame.consoleId = 1; } if (row.Cells["colBoxed"].Value != null) { newGame.boxed = Convert.ToBoolean(row.Cells["colBoxed"].Value); } else { newGame.boxed = false; } if (row.Cells["colContainer"].Value != null) { newGame.containerId = db.GetContainerID(row.Cells["colContainer"].Value.ToString()); } else { newGame.containerId = 1; } // Insert it if (row.Cells["colGID"].Value == null) { db.InsertGame(newGame); } // Update it else { // Make sure GID is valid if (row.Cells["colGID"].Value != null && Int32.TryParse(row.Cells["colGID"].Value.ToString(), out test)) { newGame.gid = Convert.ToInt32(row.Cells["colGID"].Value); db.UpdateGame(newGame); } } } } } // Consoles tab is selected // Performs similarly to the above, so comments are lax else if (tabMain.SelectedTab.Name == "tpConsoles") { foreach (DataGridViewRow row in gridConsoles.Rows) { if (CheckEmptyRow(row)) { continue; } if (row.Cells["colCID"].Value == null || consolesChangedRows.Contains(row.Cells["colCID"].RowIndex)) { DBHandler.ConsoleInfo newConsole = new DBHandler.ConsoleInfo(); if (row.Cells["colConsoleName"].Value != null) { newConsole.name = row.Cells["colConsoleName"].Value.ToString(); } else { newConsole.name = "[UNTITLED]"; } if (row.Cells["colManufacturer"].Value != null) { newConsole.manufacturer = row.Cells["colManufacturer"].Value.ToString(); } else { newConsole.manufacturer = ""; } if (row.Cells["colConsoleContainer"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) { newConsole.containerId = Convert.ToInt32(row.Cells["colConsoleContainer"].Value); } else { newConsole.containerId = 0; } if (row.Cells["colSwitchbox"].Value != null) { newConsole.switchBoxId = db.GetSwitchboxID(row.Cells["colSwitchbox"].Value.ToString()); } else { newConsole.switchBoxId = 0; } if (row.Cells["colSwitchboxNo"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) { newConsole.switchBoxNo = Convert.ToInt32(row.Cells["colSwitchboxNo"].Value); } else { newConsole.switchBoxNo = 0; } if (row.Cells["colCID"].Value == null) { db.InsertConsole(newConsole); } else { if (row.Cells["colCID"].Value != null && Int32.TryParse(row.Cells["colCID"].Value.ToString(), out test)) { newConsole.cid = Convert.ToInt32(row.Cells["colCID"].Value); db.UpdateConsole(newConsole); } } } } } // Containers tab is selected else if (tabMain.SelectedTab.Name == "tpContainers") { foreach (DataGridViewRow row in gridContainers.Rows) { if (CheckEmptyRow(row)) { continue; } if (row.Cells["colConID"].Value == null || containersChangedRows.Contains(row.Cells["colConID"].RowIndex)) { DBHandler.ContainerInfo newContainer = new DBHandler.ContainerInfo(); if (row.Cells["colContainerName"].Value != null) { newContainer.name = row.Cells["colContainerName"].Value.ToString(); } else { newContainer.name = "[UNTITLED]"; } if (row.Cells["colConID"].Value == null) { db.InsertContainer(newContainer); } else { if (row.Cells["colConID"].Value != null && Int32.TryParse(row.Cells["colConID"].Value.ToString(), out test)) { newContainer.con_id = Convert.ToInt32(row.Cells["colConID"].Value); db.UpdateContainer(newContainer); } } } } } // Switchbox tab is selected else if (tabMain.SelectedTab.Name == "tpSwitchboxes") { foreach (DataGridViewRow row in gridSwitchboxes.Rows) { if (CheckEmptyRow(row)) { continue; } if (row.Cells["colSID"].Value == null || switchboxesChangedRows.Contains(row.Cells["colSID"].RowIndex)) { DBHandler.SwitchboxInfo newSwitchbox = new DBHandler.SwitchboxInfo(); if (row.Cells["colSwitchboxName"].Value != null) { newSwitchbox.name = row.Cells["colSwitchboxName"].Value.ToString(); } else { newSwitchbox.name = "[UNTITLED]"; } if (row.Cells["colNumSwitches"].Value != null && Int32.TryParse(row.Cells["colConsoleContainer"].Value.ToString(), out test)) { newSwitchbox.numSwitches = Convert.ToInt32(row.Cells["colNumSwitches"].Value); } else { newSwitchbox.name = "[UNTITLED]"; } if (row.Cells["colSID"].Value == null) { db.InsertSwitchbox(newSwitchbox); } else { if (row.Cells["colSID"].Value != null && Int32.TryParse(row.Cells["colSID"].Value.ToString(), out test)) { newSwitchbox.sid = Convert.ToInt32(row.Cells["colSID"].Value); db.UpdateSwitchbox(newSwitchbox); } } } } } RefreshLists(); }