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.

This commit is contained in:
2017-12-11 19:55:46 -06:00
parent 13af4ff791
commit 6ec32afc92
+111 -127
View File
@@ -1309,10 +1309,10 @@ namespace AdvertsingProfitControl
projectionsDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving; projectionsDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving;
projectionsDataGridView.RowsRemoved += ProjectionRowRemoved; projectionsDataGridView.RowsRemoved += ProjectionRowRemoved;
projectionsDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; projectionsDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing;
//projectionsDataGridView.MouseMove += dataGridView1_MouseMove; projectionsDataGridView.MouseMove += DataGridViewMouseMove;
//projectionsDataGridView.MouseDown += dataGridView1_MouseDown; projectionsDataGridView.MouseDown += DataGridViewMouseDown;
//projectionsDataGridView.DragOver += dataGridView1_DragOver; projectionsDataGridView.DragOver += DataGridViewDragOver;
//projectionsDataGridView.DragDrop += dataGridView1_DragDrop; projectionsDataGridView.DragDrop += DataGridViewDragDrop;
} }
else else
{ {
@@ -1325,10 +1325,10 @@ namespace AdvertsingProfitControl
projectionsDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; projectionsDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving;
projectionsDataGridView.RowsRemoved -= ProjectionRowRemoved; projectionsDataGridView.RowsRemoved -= ProjectionRowRemoved;
projectionsDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; projectionsDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing;
//projectionsDataGridView.MouseMove -= dataGridView1_MouseMove; projectionsDataGridView.MouseMove -= DataGridViewMouseMove;
//projectionsDataGridView.MouseDown -= dataGridView1_MouseDown; projectionsDataGridView.MouseDown -= DataGridViewMouseDown;
//projectionsDataGridView.DragOver -= dataGridView1_DragOver; projectionsDataGridView.DragOver -= DataGridViewDragOver;
//projectionsDataGridView.DragDrop -= dataGridView1_DragDrop; projectionsDataGridView.DragDrop -= DataGridViewDragDrop;
} }
} }
@@ -1810,6 +1810,10 @@ out double beginningBinCount, out string _))
inventoryDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving; inventoryDataGridView.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving;
inventoryDataGridView.RowsRemoved += InventoryRowRemoved; inventoryDataGridView.RowsRemoved += InventoryRowRemoved;
inventoryDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; inventoryDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing;
inventoryDataGridView.MouseMove += DataGridViewMouseMove;
inventoryDataGridView.MouseDown += DataGridViewMouseDown;
inventoryDataGridView.DragOver += DataGridViewDragOver;
inventoryDataGridView.DragDrop += DataGridViewDragDrop;
} }
else else
{ {
@@ -1822,6 +1826,10 @@ out double beginningBinCount, out string _))
inventoryDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; inventoryDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving;
inventoryDataGridView.RowsRemoved -= InventoryRowRemoved; inventoryDataGridView.RowsRemoved -= InventoryRowRemoved;
inventoryDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; 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.UserDeletingRow += UpdateUsedAdItemCollectionOnRowRemoving;
actualSalesDataGridView.RowsRemoved += ActualSalesRowRemoved; actualSalesDataGridView.RowsRemoved += ActualSalesRowRemoved;
actualSalesDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing; actualSalesDataGridView.EditingControlShowing += DisplayAutoCompleteOnEditingControlShowing;
actualSalesDataGridView.MouseMove += DataGridViewMouseMove;
actualSalesDataGridView.MouseDown += DataGridViewMouseDown;
actualSalesDataGridView.DragOver += DataGridViewDragOver;
actualSalesDataGridView.DragDrop += DataGridViewDragDrop;
} }
else else
{ {
@@ -2053,6 +2065,10 @@ out double beginningBinCount, out string _))
actualSalesDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving; actualSalesDataGridView.UserDeletingRow -= UpdateUsedAdItemCollectionOnRowRemoving;
actualSalesDataGridView.RowsRemoved -= ActualSalesRowRemoved; actualSalesDataGridView.RowsRemoved -= ActualSalesRowRemoved;
actualSalesDataGridView.EditingControlShowing -= DisplayAutoCompleteOnEditingControlShowing; 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; projectionsDataGridView.Rows[index].Cells[1].Value = adSpecial.Name;
index++; index++;
} }
if (_adSpecialIndex == -1)
{
_usedAdItems[0].Add(projection.AdItem.Name);
}
else
{
_usedAdItems[1].Add(projection.AdItem.Name);
}
projectionsDataGridView.Rows.Add(); projectionsDataGridView.Rows.Add();
projectionsDataGridView.Rows[index].Cells[0].Value = projection.Id; projectionsDataGridView.Rows[index].Cells[0].Value = projection.Id;
projectionsDataGridView.Rows[index].Cells[1].Value = projection.AdItem.Name; 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. //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 //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; var dataGridView = (DataGridView) sender;
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 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; var dataGridView = (DataGridView)sender;
// Get the index of the item the mouse is below. // Get the index of the item the mouse is below.
@@ -4588,12 +4612,12 @@ out double beginningBinCount, out string _))
_dragBoxFromMouseDown = Rectangle.Empty; _dragBoxFromMouseDown = Rectangle.Empty;
} }
private void dataGridView1_DragOver(object sender, DragEventArgs e) private void DataGridViewDragOver(object sender, DragEventArgs e)
{ {
e.Effect = DragDropEffects.Move; 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. //Check if the item being dropped is actually a row object.
if (sender.GetType() != typeof(DataGridView)) return; if (sender.GetType() != typeof(DataGridView)) return;
@@ -4622,142 +4646,102 @@ out double beginningBinCount, out string _))
{ {
_rowIndexOfItemUnderMouseToDrop--; _rowIndexOfItemUnderMouseToDrop--;
} }
//Basic verification completed, now un-subscribe from the row add events. //For now prevent the ad special row from being moved.
projectionsDataGridView.RowsAdded -= DisplayRowNumbers; if (rowToMove.Index == _adSpecialIndex)
inventoryDataGridView.RowsAdded -= DisplayRowNumbers; {
actualSalesDataGridView.RowsAdded -= DisplayRowNumbers; return;
//And disable the row removed event, can't risk anything here. }
projectionsDataGridView.RowsRemoved -= ProjectionRowRemoved; ToggleProjectionTableEvents(false);
inventoryDataGridView.RowsRemoved -= InventoryRowRemoved; ToggleInventoryEvents(false);
actualSalesDataGridView.RowsRemoved -= ActualSalesRowRemoved; ToggleActualSalesEvents(false);
//Now check to see if the row is a header, member or ad special row.
//dataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); MoveDraggedRow();
//dataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, rowToMove);
DragNonAttributedRow(); 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 //Rows to be moved
var projectionRowToMove = projectionsDataGridView.Rows[_rowIndexFromMouseDown]; var projectionRowToMove = projectionsDataGridView.Rows[_rowIndexFromMouseDown];
var inventoryRowToMove = inventoryDataGridView.Rows[_rowIndexFromMouseDown]; var inventoryRowToMove = inventoryDataGridView.Rows[_rowIndexFromMouseDown];
var actualSalesRowToMove = actualSalesDataGridView.Rows[_rowIndexFromMouseDown]; var actualSalesRowToMove = actualSalesDataGridView.Rows[_rowIndexFromMouseDown];
//Assume that the row to move is a regular row with now attributes. //The ad item being moved
//Begin by checking the attributes of the target row. (R -> R || A) var adItem = projectionsDataGridView.Rows[_rowIndexFromMouseDown].Cells[(int)TableGroupParser.SalesTableColumns.AdItem].EditedFormattedValue;
//Note: this if case will capture the ad special row as well. //Get the smallest and biggest row numbers.
//Check to see where the row is being dropped. This only needs to be considered var min = _rowIndexFromMouseDown > _rowIndexOfItemUnderMouseToDrop
//if one row is greater then or less then the ad special index (assuming its set) ? _rowIndexOfItemUnderMouseToDrop
//and the target row is the opposite of the row being moved. : _rowIndexFromMouseDown;
if (_adSpecialIndex != -1) 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) MessageBox.Show(@"The ad special group already contains the ad item " + adItem + @".", @"Duplicate Ad Items Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
{ return;
//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());
}
} }
//This row is simply a regular row, so simply drop the row to move into place. //Move the ad item out of the regular group and into the ad special group.
projectionsDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); _usedAdItems[0].Remove(adItem.ToString());
projectionsDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, projectionRowToMove); _usedAdItems[1].Add(adItem.ToString());
inventoryDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); //Moving the row to move into the Ad Special group.
inventoryDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, inventoryRowToMove); _adSpecialIndex--;
actualSalesDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); }
actualSalesDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, actualSalesRowToMove); 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.
private void DragRow() if (_usedAdItems[0].Contains(adItem))
{
//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)
{ {
if (_rowIndexFromMouseDown < _adSpecialIndex && _rowIndexOfItemUnderMouseToDrop > _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;
//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--;
}
} }
//This particular situation is similar to the non attributed row being dropped on a like row. //Move the ad item out of the ad special group and into the regular group.
//Except that the target row has to have its "Header Row" attribute cleared, and color cleared. _usedAdItems[1].Remove(adItem.ToString());
//Then the row to move needs to be given that attribute. _usedAdItems[0].Add(adItem.ToString());
////TODO: may not be necessary //Moving the row to move OUT of the Ad Special group.
//projectionsDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); _adSpecialIndex++;
//inventoryDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); }
//actualSalesDataGridView.Rows[targetRow.Index].DefaultCellStyle.BackColor = default(Color); else if (_rowIndexOfItemUnderMouseToDrop == _adSpecialIndex)
//Now move the rows around. {
projectionsDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); //Check to see if the row from the mouse down is already in the add special group.
projectionsDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, projectionRowToMove); if (!_usedAdItems[1].Contains(adItem) && _rowIndexFromMouseDown < _adSpecialIndex)
inventoryDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); {
inventoryDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, inventoryRowToMove); //Move the ad item out of the regular and into the ad special group.
actualSalesDataGridView.Rows.RemoveAt(_rowIndexFromMouseDown); _usedAdItems[0].Remove(adItem.ToString());
actualSalesDataGridView.Rows.Insert(_rowIndexOfItemUnderMouseToDrop, actualSalesRowToMove); _usedAdItems[1].Add(adItem.ToString());
//Move the ad special row up by one row if the user tries dropping a row on it.
//And mark the affected rows as dirty so their positions will be updated on save. _adSpecialIndex--;
// MarkRowRangeForEditing(_rowIndexFromMouseDown, targetIndex); }
//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) private void MarkRowRangeForEditing(int startingIndex, int endingIndex)
{ {
var parser = new RowParser();
//Mark the changed rows as dirty. //Mark the changed rows as dirty.
for (var i = startingIndex; i <= endingIndex; i++) for (var i = startingIndex; i <= endingIndex; i++)
{ {
projectionsDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString(); projectionsDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString();
inventoryDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString(); inventoryDataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString();
actualSalesDataGridView.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; continue;
} }