如何检查是否有两个连续的行和空行,直到填充行使用DataRow C#?

时间:2018-05-08 18:15:11

标签: c# excel datarow exceldatareader

我有一个代码告诉用户他们留下哪些单元格为空或者哪些行没有填写但是现在用户希望我允许他们离开至少两行 EMPTY < / strong> 连续

所以我需要更改我的验证码才能在这些场景中工作:

  1. 如果只有一个空行,则记录错误,然后转到下一行。
  2. 如果有两个连续的空行,则转到下一行检查它是否为空,如果是空日志错误,如果没有做好跳舞。
  3. 对于下面的示例,我的代码将确认ROW 3为空并将其记录为错误并转到下一行,但它不会跳过两个连续的空行。但我希望它跳过并转到下一行。

    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."
                });
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

我认为这样可以解决问题

    // 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();
    }

答案 1 :(得分:0)

我可以建议你在完成循环之后记录行:

 public class Command
 {
    public string name;
    public string description;
    public bool hasParameter;

    Command()
    {

    }
 }