using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace AdvertsingProfitControl { public partial class frmAddRecord : Form { public frmAddRecord() { InitializeComponent(); } private void frmAddRecord_Load(object sender, EventArgs e) { ConstructForm(); //addRecordAPCMainDataGridView.UserAddedRow += CheckForGroupedItems; addRecordAPCMainDataGridView.RowLeave += CheckForGroupedItems; //addRecordAPCMainDataGridView.UserDeletedRow += CheckForGroupedItems; } void addRecordAPCMainDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { bool isGroupMember = false; textBoxMenu.Text = e.RowIndex.ToString() + ", " + e.ColumnIndex.ToString(); foreach (DataGridViewCell cell in addRecordAPCMainDataGridView.Rows[e.RowIndex].Cells) { if (cell.ColumnIndex > 0 && cell.Value != null || cell.ColumnIndex > 0 && cell.EditedFormattedValue != null) { isGroupMember = false; break; } else if (cell.ColumnIndex == 0 && cell.Value != null || cell.ColumnIndex > 0 && cell.EditedFormattedValue != null) { isGroupMember = true; } else { isGroupMember = true; } } if (isGroupMember) { if (addRecordAPCMainDataGridView.Rows[e.RowIndex - 1].DefaultCellStyle.BackColor != Color.LightBlue) { addRecordAPCMainDataGridView.Rows[e.RowIndex - 1].DefaultCellStyle.BackColor = Color.LightBlue; addRecordAPCMainDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue; } else { addRecordAPCMainDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue; } } else { addRecordAPCMainDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; } } private void CheckForGroupedItems(object sender, DataGridViewCellEventArgs e) { } private void RowsRemovedEvent(object sender, DataGridViewRowsRemovedEventArgs e) { } private void RowAddedEvent(object sender, DataGridViewRowsAddedEventArgs e) { } private void ConstructForm() { string[] columnNames = { "AdItem", "PrSold", "PrSalePrice", "PrTotalSales", "PrCost", "PrProfitReturn", "PrTotalProfitReturn", "InvBeginingInventory", "InvReceived", "InvTotal", "InvEndingInventory", "AcSold", "AcSalePrice", "AcTotalSales", "AcCost", "AcProfitReturn", "AcTotalProfitReturn"}; string[] columnHeaderText = { "Ad Item", "Sold", "Sale Price", "Total Sales", "Cost", "Profit Return", "Total Profit Returned", "Beginning Inventory", "Received", "Total", "Ending Inventory", "Sold", "Sale Price", "Total Sales", "Cost", "Profit Return", "Total Profit Return" }; int i = 0; foreach (string columnName in columnNames) { DataGridViewColumn column = new DataGridViewTextBoxColumn(); column.Name = columnName; column.HeaderText = columnHeaderText[i]; addRecordAPCMainDataGridView.Columns.Add(column); i++; } } private void addRecEditAddRecordMainMenu_Click(object sender, EventArgs e) { if (dateMaskedTextBox.MaskCompleted) { AddRecordToDatabase(); } else { MessageBox.Show("Please enter a date."); } } private void AddRecordToDatabase() { RetrieveTables tableData = new RetrieveTables(); List projectedTableData = new List(); List actualTableData = new List(); List inventoryTableData = new List(); List comments = new List(); string adItemID = ""; int columnIndex = 0; int rowIndex = 0; string dateID = tableData.DateID(dateMaskedTextBox.Text); foreach (DataGridViewRow row in addRecordAPCMainDataGridView.Rows) { columnIndex = 0; foreach (DataGridViewColumn column in addRecordAPCMainDataGridView.Columns) { if (column.Name == "AdItem") { try { adItemID = tableData.GetAdItemID(addRecordAPCMainDataGridView[columnIndex, rowIndex].Value.ToString()); } catch { MessageBox.Show("Please specify an ad item in row " + rowIndex.ToString() + ".", "No Ad Item"); return; } } else if (column.Name.StartsWith("Pr")) { try { projectedTableData.Add(addRecordAPCMainDataGridView[columnIndex, rowIndex].Value.ToString()); } catch { projectedTableData.Add("0"); } } else if (column.Name.StartsWith("Inv")) { try { inventoryTableData.Add(addRecordAPCMainDataGridView[columnIndex, rowIndex].Value.ToString()); } catch { inventoryTableData.Add("0"); } } else if (column.Name.StartsWith("Ac")) { try { actualTableData.Add(addRecordAPCMainDataGridView[columnIndex, rowIndex].Value.ToString()); } catch { actualTableData.Add("0"); } } else if (column.Name == "comments") { try { comments.Add(addRecordAPCMainDataGridView[columnIndex, rowIndex].Value.ToString()); } catch { comments.Add(""); } } columnIndex++; } AddToDataBase(projectedTableData, dateID, adItemID, "ProjectedSales"); AddToDataBase(actualTableData, dateID, adItemID, "ActualSales"); AddToDataBase(inventoryTableData, dateID, adItemID, "Inventory"); AddToDataBase(comments, dateID, adItemID, "Comment"); rowIndex++; } } private void AddToDataBase(List values, string dateID, string adItemID, string table) { int i = 0; int length = values.Count; AddRecord addRecord = new AddRecord(); string insertValues = "("; foreach (string parameter in values) { if (length == 1 || length == 0) { if (length == 0) { return; } else { insertValues += "'" + parameter + "')"; } } else { if (i < length && i == 0) { insertValues += "'" + parameter + "'"; } else if (i == length - 1) { insertValues += ", '" + parameter + "', '" + adItemID + "', '" + dateID + "')"; } else if (i < length && i != 0) { insertValues += ", '" + parameter + "'"; } } i++; } MessageBox.Show(insertValues); insertValues = @"INSERT INTO " + table + " VALUES " + insertValues; addRecord.ExecuteQuery(insertValues); } } }