如何使用DataRow C#检查是否有两个连续行和空行,直到有一个填充行为止?
我有一个代码,告诉用户哪些单元格是空的,或者哪些行没有填写,但现在用户希望我允许他们连续离开至少两行 因此,我需要更改我的验证代码以在以下情况下工作:如何使用DataRow C#检查是否有两个连续行和空行,直到有一个填充行为止?,c#,excel,datarow,exceldatareader,C#,Excel,Datarow,Exceldatareader,我有一个代码,告诉用户哪些单元格是空的,或者哪些行没有填写,但现在用户希望我允许他们连续离开至少两行 因此,我需要更改我的验证代码以在以下情况下工作: 如果只有一个空行,则记录错误,然后转到下一行 如果有两个连续的空行,则转到下一行检查它是否为null,如果为null,则记录错误,如果不是,则跳快乐舞 对于下面的示例,我的代码将确认第3行为空,并将其记录为错误并转到下一行,但它不会跳过两个连续的空行。但我想让它跳到下一排 Row 1 | Filled Row 2 | Filled Row 3 |
第3行
为空,并将其记录为错误并转到下一行,但它不会跳过两个连续的空行。但我想让它跳到下一排
Row 1 | Filled
Row 2 | Filled
Row 3 |
Row 4 | Filled
Row 5 |
Row 6 |
Row 7 | Filled
我认为这将解决问题
// 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();
}
//检查数据行是否为空
//dt=要检查的数据表
//index=将要检查的行的索引
公共bool isRowEmpty(数据表dt,int索引)
{
//检查索引是否存在,如果不存在,则返回false
//这意味着该行“不是空的”
如果(索引>=dt.Rows.Count | |索引<0)
返回false;
//吵架
DataRow dr=dt.行[索引];
//空列的数量
int-emptyQt=0;
//运行thourgh列以检查其中是否有空列
for(int i=0;i
我可以建议您在完成循环后记录行,如下所示:
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()))
var errorRows=新列表();
对于(var i=0;istring.IsNullOrEmpty(cell.ToString()))
{
if(errorRows.Contains(i-1))
{
//当上一行为空时,将其从列表中删除
错误行。删除(i-1);
//在这里,当前行也不会添加到列表中
}
其他的
{
添加(i);
}
}
}
日志(errorRows.Select(c=>(c+1.ToString()))
所以在一天结束时,如果有两行以上的空行,它应该抛出一个警告,对吗?连续两行还是总共两行?非常好的问题!连续两行@Marco@Andre.Santarosa不,事实上!如果它找到两个连续的空行,则跳过它,转到下一行,依此类推。@Marco,这样用户可以留下两个连续的空行,而这些不应被记录为错误。
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()))