Разрешить пользователям оставлять полностью пустыми одну или две пустые строки.Если какая-либо из ячеек в строках была заполнена, попросите пользователей заполнить оставшиеся ячейки и сообщить им, в какой строке / строке эта ячейка находится для заполнения.
Идеальная логика для реализации такова: если найдена пустая строка, пропустите ее и перейдите к следующей строке и найдите, не заполнена ли еще какая-либо ячейка, если найдена пустая, пропустите переход к следующей.
У меня есть два класса.Класс ниже проверяет, является ли строка полностью пустой.
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;
}