0

I have a code that tells user which cells they have left empty or which rows they did not fill out but now users want me to allow them to leave at least two rows EMPTY consecutively.

So I need to change my validation code to work in these scenarios:

  1. If there is only one empty row, log error then go to the next row.
  2. If there are two consecutive empty rows, go to next row check if it is null, if it is null log error, if not do the happy dance.

For the below example, my code will acknowledge ROW 3 is null and will log it as an error and go to the next row but it is not skipping the two consecutive empty rows. But I want it to skip and go to the next row.

Row 1 | Filled
Row 2 | Filled
Row 3 |
Row 4 | Filled
Row 5 |
Row 6 |
Row 7 | Filled

foreach (DataRow row in data.Rows)
{
    currentRowErrors.Clear();
    if (row[0] == DBNull.Value)
    {
        cnt = cnt + 1;
        if (cnt == 2)
        {
            rowCounter = rowCounter + 2;
        }
    }

    //log errors
    if (row[0] == DBNull.Value)
    {
        if (row[0] == DBNull.Value)
        {
            model.Errors.Add(new RowErrorModel()
            {
                Row = rowCounter,
                Error = "The name cannot be blank."
            });
        }
    }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
Gerle Batde
  • 183
  • 3
  • 21

2 Answers2

0

I think this will solve the problem

    // Check if datarow is empty
    // dt = Datatable to be checked
    // index = index of the row that will be checked
    public bool isRowEmpty(DataTable dt, int index)
    {
        // check if index exists, if not returns false
        // it will means that the row is "not empty"
        if (index >= dt.Rows.Count || index < 0)
            return false;

        // Get row
        DataRow dr = dt.Rows[index];

        // Amount of empty columns
        int emptyQt = 0;
        // Run thourgh columns to check if any of them are empty
        for (int i = 0; i < dr.ItemArray.Length; i++)
        {
            // If empty, add +1 to the amount of empty columns
            if (string.IsNullOrWhiteSpace(dr.ItemArray[i].ToString()))
                emptyQt++;
        }
        // if the amount of empty columns is equals to the amount of 
        //columns, it means that the whole row is empty
        return emptyQt == dr.Table.Columns.Count;
    }


    public void ValidateDataRow()
    {
        // Run through datatable
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            // Check if the current row and the next 2 ones are empty
            if (isRowEmpty(dt, i) && isRowEmpty(dt, i + 1) && isRowEmpty(dt, i + 2))
            {
                // Throws and alert that more than 2 rows 'in a row' are empty
                Console.WriteLine("More than 2 rows are empty in a row");
                // add counter by 2 because the loop will add 1 more by itselft
                i += 2;
                continue;
            }
            else
            {
                // Check if the previous row is filled, the current is empty and the next one is filled
                // The first and the last has the operator "!" beacause if the row is empty the method to check 
                // Will return false, so we have to deny it
                if (!isRowEmpty(dt, i- 1) && isRowEmpty(dt, i) && !isRowEmpty(dt, i + 1))
                {
                    // Throw alert for single empty row
                    Console.WriteLine("Empty row" + i.ToString());
                }
            }

        }
        //DoHappyDance();
    }
Andre.Santarosa
  • 1,150
  • 1
  • 9
  • 22
0

I can suggest you to log rows after finishing your loop like this:

var errorRows = new List<int>();
for (var i = 0; i < dataTable.Rows.Count; i++)
{
    var row = dataTable.Rows[i];
    if (row.ItemArray.All(cell => string.IsNullOrEmpty(cell.ToString())))
    {
        if (errorRows.Contains(i - 1))
        {
            // When previous row is empty remove it from list
            errorRows.Remove(i - 1);
            // Here also current row will not added to list
        }
        else
        {
            errorRows.Add(i);
        }
    }
}

log(errorRows.Select(c => (c + 1).ToString()))
shA.t
  • 16,580
  • 5
  • 54
  • 111