代码之家  ›  专栏  ›  技术社区  ›  Gerle Batde

如何使用Datatable C跳过=<2个空行?

  •  0
  • Gerle Batde  · 技术社区  · 6 年前

    允许用户将一行或两行完全清空。如果行中的任何一个单元格都已填写,那么请用户填写其余单元格,并告诉他们该单元格所在的行/行。

    理想的实现逻辑是:如果发现空行,跳过它并转到下一行,然后查找是否还有单元格需要填充,如果发现空行,则跳过下一行。

    我有两门课。下面的类确保行是否完全为空。

     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 DataValidationModel Validate(DataTable data, IList<FieldModel> fields)
            {
                var fieldsSorted = fields.Where(f => f.IsInTaxonomy == true).OrderBy(f => f.TaxonomyPosition).ToList();
    
                var model = new DataValidationModel()
                {
                    Errors = new List<RowErrorModel>()
                };
    
                int rowCounter = 7;
    
                for (int i =0; i < data.Rows.Count - 1; i++) //Rows.Count - 1, 
                {
                    if (!isRowEmpty(data, rowCounter-1) && isRowEmpty(data, rowCounter) && !isRowEmpty(data, rowCounter + 1))
                        i+=1;
    
    
                    if (data.Rows[rowCounter][0] == DBNull.Value || String.IsNullOrWhiteSpace(data.Rows[i][0].ToString()))
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "The name cannot be blank."
                        });
    
                    }
                    if (data.Rows[rowCounter]["Site"] == DBNull.Value || String.IsNullOrWhiteSpace(data.Rows[i]["Site"].ToString()))
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "Site is required."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["start date"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "start date is required."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["end date"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "end date is required."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["Placement Type"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "Placement Type is required."
                        });
    
                    }
                    if (data.Rows[rowCounter]["Channel"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "Channel is required."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["Environment"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "Environment is required."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["rate type"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "rate is required when a rate type is not blank."
                        });
    
                    }
    
                    if (data.Rows[rowCounter]["units"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "units is required when a rate type is not blank."
                        });
    
                    }
                    if (data.Rows[rowCounter]["cost"] == DBNull.Value)
                    {
                        model.Errors.Add(new RowErrorModel()
                        {
                            Row = rowCounter,
                            Error = "cost is required when a rate type is not blank."
                        });
    
                    }
    
    
          model.Errors = model.Errors.OrderBy(f => f.Row).ToList();
    
                return model;
            }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Tim Schmelter    6 年前

    我给你看一个与你的附加逻辑无关的例子。

    public bool isRowEmpty(DataTable dt, int index)
    {
        DataRow row = dt.Rows[index];
        return dt.Columns.Cast<DataColumn>()
            .All(c => row.IsNull(c) || string.IsNullOrWhiteSpace(row[c].ToString()));
    }
    

    foreach for -循环你只需要使用 continue :

    for (int i = 0; i < data.Rows.Count; i++)
    {
        if (isRowEmpty(data, i))
            continue;
        // ...
    }
    
        2
  •  0
  •   Rahul    6 年前

    为什么不能使用 continue 一旦您发现根据您的注释,该行为空,如

            for (int i =0; i < data.Rows.Count - 1; i++) //Rows.Count - 1, 
            {
                if (!isRowEmpty(data, rowCounter-1) && isRowEmpty(data, rowCounter) && !isRowEmpty(data, rowCounter + 1))
                    continue; // this one here, which will jump to next iteration