【发布时间】:2017-07-11 19:43:58
【问题描述】:
我使用工具箱菜单中的标准 DataGridView。
我通过事件cellEditEnd 验证DataGridView 中的每个单元格。它看起来像这样:
private void dataGrid_CellEditEnding(object sender,DataGridCellEditEndingEventArgs e)
{
// Below I demonstrate pseudo code for brevity
if(cell.name.value is not Number){
print "Wrong cell value";
}
}
因此,在另一种形式 (WinForms) 中,我具有从 Excel 文件导入数据并将其显示在 DataGridView 中的功能。
我需要在插入每个单元格之前验证 Excel 数据。为此,我可以使用事件CellValidation。但我不想重复我在dataGrid_CellEditEnding 方法中使用的相同代码。
如何避免重复代码?
【问题讨论】:
-
是否有理由不将验证代码放入单独的函数中,并在编辑和从 excel 导入时调用它?