如果在强类型DataTable上使用自动生成的方法以编程方式添加新行,我该如何触发验证字段maxleng的自定义验证?
我的客户(C#)
DAL.ImportMarcDataSet.PublicationsRow newRow = importMarcDataSet.Publications.NewPublicationsRow();
newRow.CallNumber ="QA76.76.A65";
newRow.Title = "Programming WCF services";
newRow.ISBN = "0596526997";
importMarcDataSet.Publications.AddPublicationsRow(newRow);
我的数据访问层(VB)
Partial Class ImportMarcDataSet
Partial Class PublicationsDataTable
Private Sub CallNumberMaxLength(ByVal pRow As PublicationsRow)
If pRow.CallNumber.Length > 25 Then
pRow.SetColumnError("CallNumber", "The value entered is over the maximum length")
Else
pRow.SetColumnError("CallNumber", "")
End If
End Sub
'this event is ok when user made changes to the CallNumber column of the current row
Private Sub PublicationsDataTable_ColumnChanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanged
If e.Column Is Me.CallNumberColumn Then
CallNumberMaxLength(e.Row)
End If
End Sub
End Class
End Class
答案 0 :(得分:1)
您可以处理表格的RowChanging
事件。当DataRowChangeEventArgs.Action
等于Add
或其中一项更改...操作时,请进行验证。
我做了很长时间以来,但我相信如果需要,您甚至可以通过CancelEdit
上的DataRowChangeEventArgs.Row
取消修改。查看文档。请参阅http://msdn.microsoft.com/en-us/library/w9y9a401.aspx上的处理DataTable事件(ADO.NET)。
TableNewRow
无效,因为只有在调用NewRow
时才会提出。{/ p>