|
-
Aug 25th, 2011, 10:35 AM
#1
Thread Starter
Member
Re: Problem with data-bound controls validation
Doing it manually would probably be better, yes, but that's exactly what I'm avoiding since I have about 50 different fields on that single table so creating them automatically saves a whole lot of work.
This is what I came up with, probably not the best solution, but it works:
VB.NET Code:
Private Sub GenericTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) If sender.Text.Length = 0 Then sender.CausesValidation = False Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView) currentRow.BeginEdit() currentRow(sender.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value currentRow.EndEdit() sender.CausesValidation = True End If End Sub
Thank you for your help Kevin.
-
Aug 25th, 2011, 11:05 AM
#2
Re: Problem with data-bound controls validation
 Originally Posted by mf12
Doing it manually would probably be better, yes, but that's exactly what I'm avoiding since I have about 50 different fields on that single table so creating them automatically saves a whole lot of work.
This is what I came up with, probably not the best solution, but it works:
VB.NET Code:
Private Sub GenericTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
If sender.Text.Length = 0 Then
sender.CausesValidation = False
Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
currentRow.BeginEdit()
currentRow(sender.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
currentRow.EndEdit()
sender.CausesValidation = True
End If
End Sub
Thank you for your help Kevin.
Your welcome for the help.
Might I suggest trying to set Option Strict On. Although your code above works it is late binding meaning if there was a possibilty of an issue with the code having Option Strict On gives you a better chance at correcting it prior to running the app.
So for the code above the following would be fine with Option Strict One
Code:
Private Sub GenericTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim CurrentBox = DirectCast(sender, TextBox)
If CurrentBox.Text.Length = 0 Then
CurrentBox.CausesValidation = False
Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
currentRow.BeginEdit()
currentRow(CurrentBox.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
currentRow.EndEdit()
CurrentBox.CausesValidation = True
End If
End Sub
My original suggestion would then be (for generic usage)
Code:
Private Sub GenericDecimalTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim CurrentBox = DirectCast(sender, TextBox)
If CurrentBox.Text.Equals("") Then
Dim currentRow As DataRowView = TryCast(bsDecimals.Current, DataRowView)
Dim FieldName As String = CurrentBox.DataBindings.Item("Text") _
.BindingMemberInfo.BindingField
If chkUseNull.Checked Then
currentRow.Row(FieldName) = System.DBNull.Value
Else
currentRow.Row(FieldName) = 0
End If
End If
End Sub
-
Aug 26th, 2011, 07:10 AM
#3
Thread Starter
Member
Re: Problem with data-bound controls validation
 Originally Posted by kevininstructor
Your welcome for the help.
Might I suggest trying to set Option Strict On. Although your code above works it is late binding meaning if there was a possibilty of an issue with the code having Option Strict On gives you a better chance at correcting it prior to running the app.
So for the code above the following would be fine with Option Strict One
Code:
Private Sub GenericTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim CurrentBox = DirectCast(sender, TextBox)
If CurrentBox.Text.Length = 0 Then
CurrentBox.CausesValidation = False
Dim currentRow As DataRowView = TryCast(EnsaiosBindingSource.Current, DataRowView)
currentRow.BeginEdit()
currentRow(CurrentBox.DataBindings.Item("Text").BindingMemberInfo.BindingField) = System.DBNull.Value
currentRow.EndEdit()
CurrentBox.CausesValidation = True
End If
End Sub
My original suggestion would then be (for generic usage)
Code:
Private Sub GenericDecimalTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim CurrentBox = DirectCast(sender, TextBox)
If CurrentBox.Text.Equals("") Then
Dim currentRow As DataRowView = TryCast(bsDecimals.Current, DataRowView)
Dim FieldName As String = CurrentBox.DataBindings.Item("Text") _
.BindingMemberInfo.BindingField
If chkUseNull.Checked Then
currentRow.Row(FieldName) = System.DBNull.Value
Else
currentRow.Row(FieldName) = 0
End If
End If
End Sub
Thanks lol, that was some code I had just written in a rush.
Today, after doing some more research, I found this:
http://stackoverflow.com/questions/4...921107#4921107
http://msdn.microsoft.com/en-us/libr...ndp1_v3_topic5
This is the most important part of that stackoverflow response:
Once the data source has been updated (to DBNull), the binding mechanism will attempt to then repopulate the Textbox with the newly updated data source value. When the underlying data source value is DBNull, the value used for the bound control is governed by the Binding class's NullValue property.
So, as a TextBox's Text property is a String and it gets assigned a null value, an error occurs.
The solution is to change the BindigClass' NullValue property and set it to a valid value (i.e. String.Empty).
This is quite easy when we manually add the bindings to a few controls in our code but I wonder if there's a way to do the same with controls and bindings created by the form designer...
Last edited by mf12; Aug 26th, 2011 at 07:14 AM.
-
Aug 26th, 2011, 08:50 AM
#4
Re: Problem with data-bound controls validation
 Originally Posted by mf12
Thanks lol, that was some code I had just written in a rush.
Today, after doing some more research, I found this:
http://stackoverflow.com/questions/4...921107#4921107
http://msdn.microsoft.com/en-us/libr...ndp1_v3_topic5
This is the most important part of that stackoverflow response:
So, as a TextBox's Text property is a String and it gets assigned a null value, an error occurs.
The solution is to change the BindigClass' NullValue property and set it to a valid value (i.e. String.Empty).
This is quite easy when we manually add the bindings to a few controls in our code but I wonder if there's a way to do the same with controls and bindings created by the form designer...
In regards to working with the form designer, I honestly have no idea as I never done things this way, only manually. Have you tried using the generic method?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|