using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Linq; namespace AdvertsingProfitControl { internal class RowParsing { public static List AdSpecialGroups = new List(); public string CheckForGroupKeyWord(string cellContents) { var keyWord = "NoGroupFound"; var query = from adI in AdSpecialGroups where adI.Equals(cellContents, StringComparison.InvariantCultureIgnoreCase) select adI; var enumerable = query as string[] ?? query.ToArray(); if (enumerable.ToArray().Length > 0) { //Grab the first object in the array and return it. keyWord = enumerable.ToArray().First(); } return keyWord; } /// /// Determines a row's status. /// /// The row from a DataGridView object to be examined. /// The row's status, such as HeaderRow or NewRow. public RowAttribute GetRowAttribute(DataGridViewRow row) { //IF the supplied row is null, then simply declare it an IncompleteRow. if (row == null) { return RowAttribute.IncompleteRow; } //IF the supplied row is a new row, by the definition of the DataGridViewRow class, then return NewRow. if (row.IsNewRow) { return RowAttribute.NewRow; } //Determine the range to use during the looping process. var headerCellIndex = row.Cells.Count == Enum.GetNames(typeof(SalesTableColumns)).Length ? (int)SalesTableColumns.IsHeaderRow : (int)InventoryTableColumns.IsHeaderRow; var rowContents = new List(); var rowAttribute = RowAttribute.MemberRow; //Get the contents of the row that's being examined and add then to a List array. var i = 0; foreach (DataGridViewCell cell in row.Cells) { if (cell.ColumnIndex >= headerCellIndex || cell.ColumnIndex == 0) { i++; continue;} //Strip all whitespace characters from the cell, this includes vertical tabs, newlines, and any number of spaces. var cellContentsStripped = new string(cell.EditedFormattedValue.ToString().Where(c => !char.IsWhiteSpace(c)).ToArray()); //Now remove all zeros, including any decimal points as these values are meaningless. cellContentsStripped = cellContentsStripped.Replace("0", ""); cellContentsStripped = cellContentsStripped.Replace(".", ""); if (cellContentsStripped != "") { rowContents.Add(cell.EditedFormattedValue.ToString()); } //If the first cell contains nothing, then return as IncompleteRow. else if (cellContentsStripped == "" && i == (int) SalesTableColumns.AdItem) { return RowAttribute.IncompleteRow; } i++; } //With all meaningless content removed, only actual data should be here. So if there are more then one items, than that means there are more then one //cells with meaningful data in them. Which means that this row can be a header row with member rows under it. if (rowContents.Count > 1) { rowAttribute = RowAttribute.HeaderRow; } else if (rowContents.Count == 1) //Check to see if this row is intended to be for a special ad. { var keyWord = CheckForGroupKeyWord(rowContents[0]); //IF a keyword is found then declare this row an AdSpecialRow. if (keyWord != "NoGroupFound") { return RowAttribute.AdSpecialRow; } } return rowAttribute; } public RowAttribute GetRowAttribute(object[] row) { var rowAttribute = RowAttribute.MemberRow; return rowAttribute; } public void PaintDataGridViewRowGroups(DataGridView dataGridView, DataGridView companionDataGridView = null, int startIndex = 0, bool stopOnFirstFullGroupFound = false) { } /// /// Checks for the presents of a repeat command using the Reg-ex engine. /// If a repeat command is found it returns the line number the command /// specifies to repeat. /// /// The contents of Cell[0] in the row being parsed. /// Line number to be repeated, -1 if no command is found. public int CheckForRepeatCommand(string rowCommandField) { const string repeatCommandPattern = @"^repeat( line)?:? [0-9]"; //Reference sheet: https://msdn.microsoft.com/en-us/library/az24scfc.aspx var rgx = new Regex(repeatCommandPattern, RegexOptions.IgnoreCase); var lineNumber = -1; if (!rgx.IsMatch(rowCommandField)) return lineNumber; //Parse the string and retrieve the line number var numbers = Regex.Split(rowCommandField, @"\D+"); lineNumber = int.Parse(numbers[1]); return lineNumber; } } public enum RowAttribute { NewRow = 0, IncompleteRow = 1, AdSpecialRow = 2, RepeatedRow = 3, MemberRow = 4, HeaderRow = 5 } }