Data validation in WinForms using a BindingSource

I spent the better half of my workday trying to find a viable any solution for data validation in WinForms using a BindingSource.

According to the Internet (tm) nobody has ever thought of this. The best bet so far is this tiny snippet from the .NET docs, which seems to suggest that “there is no built-in way, just do it yourself, idiot!”.

Private Sub AcceptOrReject(ByVal row As DataRow)
    If ValidateRow(row) Then
    End If
End Sub

Private Function ValidateRow(ByVal thisRow As DataRow) As Boolean
    Dim isValid As Boolean = True
    Return isValid
End Function

What I really want to achieve is to receive more data than the “Column ‘Name’ does not allow ‘nulls’” message in the NoNullAllowedException.

In the end I got what I wanted by doing something like this:

Dim oSource As BindingSource = oBinding.DataSource
Dim oTableName As String = oSource.DataMember
Dim oDataSet As DataSet = oSource.DataSource
Dim oTable = oDataSet.Tables(oTableName)
Dim oColumnName As String = oBinding.BindingMemberInfo.BindingField
Dim oColumn As DataColumn = oTable.Columns.Item(oColumnName)
Return oColumn

EDIT: You can do validation by going from the controls in the form to their data-bindings to the the table in the dataset and get information like read-only from there.

Published * Last updated