如何跳过=<;2使用数据表C#的空行?
允许用户保留完全空的一行或两行。如果行中的任何一个单元格已填写完毕,则呼叫用户填写其余单元格,并告诉他们该单元格所在的行/行 理想的实现逻辑是:如果发现空行,则跳过它并转到下一行,然后查找是否还有单元格需要填充,如果发现空行,则跳过下一行 我有两节课。下面的类确保行是否完全为空如何跳过=<;2使用数据表C#的空行?,c#,datatable,datarow,C#,Datatable,Datarow,允许用户保留完全空的一行或两行。如果行中的任何一个单元格已填写完毕,则呼叫用户填写其余单元格,并告诉他们该单元格所在的行/行 理想的实现逻辑是:如果发现空行,则跳过它并转到下一行,然后查找是否还有单元格需要填充,如果发现空行,则跳过下一行 我有两节课。下面的类确保行是否完全为空 public bool isRowEmpty(DataTable dt, int index) { // check if index exists, if not returns false
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 bool isRowEmpty(数据表dt,int索引)
{
//检查索引是否存在,如果不存在,则返回false
//这意味着该行“不是空的”
如果(索引>=dt.Rows.Count | |索引<0)
返回false;
//吵架
DataRow dr=dt.行[索引];
//空列的数量
int-emptyQt=0;
//运行thourgh列以检查其中是否有空列
for(int i=0;i
使用上面的类,我确定下一个类中哪一行是空的,如果发现为空,我将跳过并转到下一行,如果发现为非空,则查找任何未填充的单元格
但是下面的代码并没有跳过完整的空行。有什么见解吗
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;
}
公共DataValidationModel验证(DataTable数据,IList字段)
{
var fieldsorted=fields.Where(f=>f.IsInTaxonomy==true).OrderBy(f=>f.TaxonomyPosition).ToList();
var模型=新的DataValidationModel()
{
错误=新列表()
};
int rowCounter=7;
对于(int i=0;i如果您发现该行根据您的注释为空,为什么不能使用
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
for(int i=0;ipublic 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()));
}
for (int i = 0; i < data.Rows.Count; i++)
{
if (isRowEmpty(data, i))
continue;
// ...
}