From 6ec32afc92eb2411a03da4a60fd2e7cf357242df Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 11 Dec 2017 19:55:46 -0600 Subject: [PATCH] Fixed a bug preventing the user from moving a member of the ad special group on to the ad special row. Prevented the ad special row from being moved by drag and drop. --- AdvertsingProfitControl/NewModifyRecord.cs | 238 ++++++++++----------- 1 file changed, 111 insertions(+), 127 deletions(-) diff --git a/AdvertsingProfitControl/NewModifyRecord.cs b/AdvertsingProfitControl/NewModifyRecord.cs index 19c21cc..91fb5a6 100644 --- a/AdvertsingProfitControl/NewModifyRecord.cs +++ b/AdvertsingProfitControl/NewModifyRecord.cs @@ -1309,10 +1309,10 @@ namespace AdvertsingProfitControl projectionsDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving; projectionsDataGridView.RowsRemoved += ProjectionRowRemoved; projectionsDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; - //projectionsDataGridView.MouseMove += dataGridView1_MouseMove; - //projectionsDataGridView.MouseDown += dataGridView1_MouseDown; - //projectionsDataGridView.DragOver += dataGridView1_DragOver; - //projectionsDataGridView.DragDrop += dataGridView1_DragDrop; + projectionsDataGridView.MouseMove += DataGridViewMouseMove; + projectionsDataGridView.MouseDown += DataGridViewMouseDown; + projectionsDataGridView.DragOver += DataGridViewDragOver; + projectionsDataGridView.DragDrop += DataGridViewDragDrop; } else { @@ -1325,10 +1325,10 @@ namespace AdvertsingProfitControl projectionsDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; projectionsDataGridView.RowsRemoved -= ProjectionRowRemoved; projectionsDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; - //projectionsDataGridView.MouseMove -= dataGridView1_MouseMove; - //projectionsDataGridView.MouseDown -= dataGridView1_MouseDown; - //projectionsDataGridView.DragOver -= dataGridView1_DragOver; - //projectionsDataGridView.DragDrop -= dataGridView1_DragDrop; + projectionsDataGridView.MouseMove -= DataGridViewMouseMove; + projectionsDataGridView.MouseDown -= DataGridViewMouseDown; + projectionsDataGridView.DragOver -= DataGridViewDragOver; + projectionsDataGridView.DragDrop -= DataGridViewDragDrop; } } @@ -1810,6 +1810,10 @@ out double beginningBinCount, out string _)) inventoryDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving; inventoryDataGridView.RowsRemoved += InventoryRowRemoved; inventoryDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; + inventoryDataGridView.MouseMove += DataGridViewMouseMove; + inventoryDataGridView.MouseDown += DataGridViewMouseDown; + inventoryDataGridView.DragOver += DataGridViewDragOver; + inventoryDataGridView.DragDrop += DataGridViewDragDrop; } else { @@ -1822,6 +1826,10 @@ out double beginningBinCount, out string _)) inventoryDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; inventoryDataGridView.RowsRemoved -= InventoryRowRemoved; inventoryDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; + inventoryDataGridView.MouseMove -= DataGridViewMouseMove; + inventoryDataGridView.MouseDown -= DataGridViewMouseDown; + inventoryDataGridView.DragOver -= DataGridViewDragOver; + inventoryDataGridView.DragDrop -= DataGridViewDragDrop; } } @@ -2041,6 +2049,10 @@ out double beginningBinCount, out string _)) actualSalesDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving; actualSalesDataGridView.RowsRemoved += ActualSalesRowRemoved; actualSalesDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; + actualSalesDataGridView.MouseMove += DataGridViewMouseMove; + actualSalesDataGridView.MouseDown += DataGridViewMouseDown; + actualSalesDataGridView.DragOver += DataGridViewDragOver; + actualSalesDataGridView.DragDrop += DataGridViewDragDrop; } else { @@ -2053,6 +2065,10 @@ out double beginningBinCount, out string _)) actualSalesDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; actualSalesDataGridView.RowsRemoved -= ActualSalesRowRemoved; actualSalesDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; + actualSalesDataGridView.MouseMove -= DataGridViewMouseMove; + actualSalesDataGridView.MouseDown -= DataGridViewMouseDown; + actualSalesDataGridView.DragOver -= DataGridViewDragOver; + actualSalesDataGridView.DragDrop -= DataGridViewDragDrop; } } @@ -3068,6 +3084,14 @@ out double beginningBinCount, out string _)) projectionsDataGridView.Rows[index].Cells[1].Value = adSpecial.Name; index++; } + if (_adSpecialIndex == -1) + { + _usedAdItems[0].Add(projection.AdItem.Name); + } + else + { + _usedAdItems[1].Add(projection.AdItem.Name); + } projectionsDataGridView.Rows.Add(); projectionsDataGridView.Rows[index].Cells[0].Value = projection.Id; projectionsDataGridView.Rows[index].Cells[1].Value = projection.AdItem.Name; @@ -4548,7 +4572,7 @@ out double beginningBinCount, out string _)) //Drag and drop functionality shamelessly ripped from StackOverflow. //Reordering rows: http://stackoverflow.com/questions/1620947/how-could-i-drag-and-drop-datagridview-rows-under-each-other/1623968#1623968 - private void dataGridView1_MouseMove(object sender, MouseEventArgs e) + private void DataGridViewMouseMove(object sender, MouseEventArgs e) { var dataGridView = (DataGridView) sender; if ((e.Button & MouseButtons.Left) == MouseButtons.Left) @@ -4565,7 +4589,7 @@ out double beginningBinCount, out string _)) } } - private void dataGridView1_MouseDown(object sender, MouseEventArgs e) + private void DataGridViewMouseDown(object sender, MouseEventArgs e) { var dataGridView = (DataGridView)sender; // Get the index of the item the mouse is below. @@ -4588,12 +4612,12 @@ out double beginningBinCount, out string _)) _dragBoxFromMouseDown = Rectangle.Empty; } - private void dataGridView1_DragOver(object sender, DragEventArgs e) + private void DataGridViewDragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } - private void dataGridView1_DragDrop(object sender, DragEventArgs e) + private void DataGridViewDragDrop(object sender, DragEventArgs e) { //Check if the item being dropped is actually a row object. if (sender.GetType() != typeof(DataGridView)) return; @@ -4622,142 +4646,102 @@ out double beginningBinCount, out string _)) { _rowIndexOfItemUnderMouseToDrop--; } - //Basic verification completed, now un-subscribe from the row add events. - projectionsDataGridView.RowsAdded -= DisplayRowNumbers; - inventoryDataGridView.RowsAdded -= DisplayRowNumbers; - actualSalesDataGridView.RowsAdded -= DisplayRowNumbers; - //And disable the row removed event, can't risk anything here. - projectionsDataGridView.RowsRemoved -= ProjectionRowRemoved; - inventoryDataGridView.RowsRemoved -= InventoryRowRemoved; - actualSalesDataGridView.RowsRemoved -= ActualSalesRowRemoved; - //Now check to see if the row is a header, member or ad special row. - //dataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - //dataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, rowToMove); - DragNonAttributedRow(); + //For now prevent the ad special row from being moved. + if (rowToMove.Index == _adSpecialIndex) + { + return; + } + ToggleProjectionTableEvents(false); + ToggleInventoryEvents(false); + ToggleActualSalesEvents(false); + + MoveDraggedRow(); + + ToggleProjectionTableEvents(); + ToggleInventoryEvents(); + ToggleActualSalesEvents(); } - private void DragNonAttributedRow() + private void MoveDraggedRow() { - //Target Rows - var projectionsTargetRow = projectionsDataGridView.Rows[_rowIndexOfItemUnderMouseToDrop]; - var inventoryTargetRow = inventoryDataGridView.Rows[_rowIndexOfItemUnderMouseToDrop]; - var actualSalesTargetRow = actualSalesDataGridView.Rows[_rowIndexOfItemUnderMouseToDrop]; //Rows to be moved var projectionRowToMove = projectionsDataGridView.Rows[_rowIndexFromMouseDown]; var inventoryRowToMove = inventoryDataGridView.Rows[_rowIndexFromMouseDown]; var actualSalesRowToMove = actualSalesDataGridView.Rows[_rowIndexFromMouseDown]; - //Assume that the row to move is a regular row with now attributes. - //Begin by checking the attributes of the target row. (R -> R || A) - //Note: this if case will capture the ad special row as well. - //Check to see where the row is being dropped. This only needs to be considered - //if one row is greater then or less then the ad special index (assuming its set) - //and the target row is the opposite of the row being moved. - if (_adSpecialIndex != -1) + //The ad item being moved + var adItem = projectionsDataGridView.Rows[_rowIndexFromMouseDown].Cells[(int)TableGroupParser.SalesTableColumns.AdItem].EditedFormattedValue; + //Get the smallest and biggest row numbers. + var min = _rowIndexFromMouseDown > _rowIndexOfItemUnderMouseToDrop + ? _rowIndexOfItemUnderMouseToDrop + : _rowIndexFromMouseDown; + var max = _rowIndexFromMouseDown > _rowIndexOfItemUnderMouseToDrop + ? _rowIndexFromMouseDown + : _rowIndexOfItemUnderMouseToDrop; + // + if(_rowIndexFromMouseDown < _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop > _adSpecialIndex) + { + //Make sure that the ad special group doesn't already contain the ad item that is about be dragged into it. + if (_usedAdItems[1].Contains(adItem)) { - if (_rowIndexFromMouseDown < _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop > _adSpecialIndex) - { - //Moving the row to move into the Ad Special group. - _adSpecialIndex--; - _usedAdItems[0].Remove( - projectionRowToMove.Cells[(int) TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - _usedAdItems[1].Add( - projectionRowToMove.Cells[(int)TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - } - else if (_rowIndexFromMouseDown > _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop < _adSpecialIndex) - { - //Moving the row to move OUT of the Ad Special group. - _adSpecialIndex++; - _usedAdItems[1].Remove( - projectionRowToMove.Cells[(int)TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - _usedAdItems[0].Add( - projectionRowToMove.Cells[(int)TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - } - else if (_rowIndexOfItemUnderMouseToDrop == _adSpecialIndex) - { - //Move the ad special row up by one row if the user tries dropping a row on it. - _adSpecialIndex--; - _usedAdItems[0].Remove( - projectionRowToMove.Cells[(int)TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - _usedAdItems[1].Add( - projectionRowToMove.Cells[(int)TableGroupParser.SalesTableColumns.AdItem] - .EditedFormattedValue.ToString()); - } + MessageBox.Show(@"The ad special group already contains the ad item " + adItem + @".", @"Duplicate Ad Items Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; } - //This row is simply a regular row, so simply drop the row to move into place. - projectionsDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - projectionsDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, projectionRowToMove); - inventoryDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - inventoryDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, inventoryRowToMove); - actualSalesDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - actualSalesDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, actualSalesRowToMove); - } - - private void DragRow() - { - //var targetRow = dataGridView.Rows[_rowIndexOfItemUnderMouseToDrop]; - //var targetIndex = targetRow.Index; //We need this as one the target row gets moved in the datagridview its index will change. - var projectionRowToMove = projectionsDataGridView.Rows[_rowIndexFromMouseDown]; - var inventoryRowToMove = inventoryDataGridView.Rows[_rowIndexFromMouseDown]; - var actualSalesRowToMove = actualSalesDataGridView.Rows[_rowIndexFromMouseDown]; - //Assume that the row to move is a regular row with now attributes. - //Begin by checking the attributes of the target row. (R -> R || A) - //Note: this if case will capture the ad special row as well. - - //If the target row is a header row, check to see where the row is being dropped. This only needs to be considered - //if one row is greater then or less then the ad special index (assuming its set) - //and the target row is the opposite of the row being moved. - if (_adSpecialIndex != -1) + //Move the ad item out of the regular group and into the ad special group. + _usedAdItems[0].Remove(adItem.ToString()); + _usedAdItems[1].Add(adItem.ToString()); + //Moving the row to move into the Ad Special group. + _adSpecialIndex--; + } + else if (_rowIndexFromMouseDown > _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop < _adSpecialIndex) + { + //Make sure that section 1 doesn't already contain the ad item that is about be dragged into it. + if (_usedAdItems[0].Contains(adItem)) { - if (_rowIndexFromMouseDown < _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop > _adSpecialIndex) - { - //Moving the row to move into the Ad Special group. - _adSpecialIndex--; - } - else if (_rowIndexFromMouseDown > _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop < _adSpecialIndex) - { - //Moving the row to move OUT of the Ad Special group. - _adSpecialIndex++; - } - else if (_rowIndexOfItemUnderMouseToDrop == _adSpecialIndex) - { - //Move the ad special row up by one row if the user tries dropping a row on it. - _adSpecialIndex--; - } + MessageBox.Show(@"The group outside of the ad special group already contains the ad item " + adItem + @".", @"Duplicate Ad Items Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; } - //This particular situation is similar to the non attributed row being dropped on a like row. - //Except that the target row has to have its "Header Row" attribute cleared, and color cleared. - //Then the row to move needs to be given that attribute. - ////TODO: may not be necessary - //projectionsDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); - //inventoryDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); - //actualSalesDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); - //Now move the rows around. - projectionsDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - projectionsDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, projectionRowToMove); - inventoryDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - inventoryDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, inventoryRowToMove); - actualSalesDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); - actualSalesDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, actualSalesRowToMove); - - //And mark the affected rows as dirty so their positions will be updated on save. - // MarkRowRangeForEditing(_rowIndexFromMouseDown, targetIndex); + //Move the ad item out of the ad special group and into the regular group. + _usedAdItems[1].Remove(adItem.ToString()); + _usedAdItems[0].Add(adItem.ToString()); + //Moving the row to move OUT of the Ad Special group. + _adSpecialIndex++; + } + else if (_rowIndexOfItemUnderMouseToDrop == _adSpecialIndex) + { + //Check to see if the row from the mouse down is already in the add special group. + if (!_usedAdItems[1].Contains(adItem) && _rowIndexFromMouseDown < _adSpecialIndex) + { + //Move the ad item out of the regular and into the ad special group. + _usedAdItems[0].Remove(adItem.ToString()); + _usedAdItems[1].Add(adItem.ToString()); + //Move the ad special row up by one row if the user tries dropping a row on it. + _adSpecialIndex--; + } + //Make sure that the ad special group doesn't already contain the ad item that is about be dragged into it. + else if (_usedAdItems[1].Contains(adItem)) + { + MessageBox.Show(@"The ad special group already contains the ad item " + adItem + @".", @"Duplicate Ad Items Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + projectionsDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); + projectionsDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, projectionRowToMove); + inventoryDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); + inventoryDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, inventoryRowToMove); + actualSalesDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); + actualSalesDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, actualSalesRowToMove); + MarkRowRangeForEditing(min, max); } private void MarkRowRangeForEditing(int startingIndex, int endingIndex) { - var parser = new RowParser(); //Mark the changed rows as dirty. for (var i = startingIndex; i <= endingIndex; i++) { projectionsDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString(); inventoryDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString(); actualSalesDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString(); - if (parser.GetRowAttribute(projectionsDataGridView.Rows[i]) == RowParser.RowAttribute.AdSpecialRow || projectionsDataGridView.Rows[i].IsNewRow) + if (i == _adSpecialIndex || projectionsDataGridView.Rows[i].IsNewRow) { continue; }