Question bug in inherited controls?

adshocker

Well-known member
Joined
Jun 30, 2007
Messages
180
Programming Experience
Beginner
hi all,

on the custom inherited control i created, i noticed that when i bind my custom property to a data column, if that data column returns a value other than Null it always return a datarowstate of modified even if there were no changes.

to produce this issue, i have below a sample code for a test inherited control

VB.NET:
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class TestControl
  Inherits TextBox

  <Bindable(True)> _
  Public Property Value() As String
    Get
      Return MyBase.Text
    End Get
    Set(ByVal value As String)
      MyBase.Text = value
    End Set
  End Property
End Class

create a form, add the usual objects necessary for data bindings, (datasource, datatable). make sure the data column bound to this TestControl will return non null values.

also create a procedure for position changed to catch the datarowstate like below...

VB.NET:
  Private Sub PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim rowstate As DataRowState = DataRowState.Unchanged
    rowstate = Me.YourDataSet.YourDataTable.Rows(Me.YourBindingSource.Position - 1).RowState
    MsgBox(rowstate.ToString)
  End Sub

Note that i used Position - 1 so that when you go to the next record, it will show you the rowstate of the previous record.

on your Form_Load add the ff:

VB.NET:
AddHandler YourBindingSource.PositionChanged, AddressOf PositionChanged

don't make any changes to the current record before you go to the next record. you will see that the rowstate will still return Modified.

can anyone share their thoughts on this?

thanks
 
anyone knows how to fix this?
 
i've now tried this on both vb 2005 express and vb 2008 express. unfortunately same behavior.

i've also tried it in vs studio 2008 pro trial and same behavior.

I've also tried creating a custom control which does not inherits any standard control but still same behavior.

i've also posted this in the microsoft forums and asking if it is a bug but have got no response yet.

i hope there's a fix to this.
 
hi all,

after lots of research, somehow i manage to find some codes that made the rowstate stay as Unchanged but the problem now is that it stays Unchanged even if there are changes.

i added the following codes:

VB.NET:
  Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)

  Public Sub OnValueChanged(ByVal e As System.EventArgs)
    RaiseEvent ValueChanged(Me, e)
  End Sub

but i'm not really that familiar with events. i just read in this thread that you need to have an event for the property.

i'm hoping someone can help create the events properly.

thanks.
 
From what I have read this is the normal behavior - like it or not. The poss fix is to roll your own:
VB.NET:
''''''''UserControl
Public Property Value() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal Value As String)
            TextBox1.Text = Value
        End Set
End Property

Private dt As New DataTable(<yourTable>)
Private dv As New DataView(dt)
Private cm As CurrencyManager

Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
''''''''Fill the table as you normally would
        dv.RowStateFilter = DataViewRowState.ModifiedCurrent
        DataGrid1.DataSource = dv
        cm = CType(BindingContext(dt), CurrencyManager)
        Dim dr As DataRow = CType(cm.Current, DataRowView).Row
        UserControl1.MyValue = dr.Item(1)
End Sub
''''''''move thru records
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
        CheckForChanges()
        cm.Position += 1
        Dim dr As DataRow = CType(cm.Current, DataRowView).Row
        UserControl1.MyValue = dr.Item(1)
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        CheckForChanges()
        cm.Position -= 1
        Dim dr As DataRow = CType(cm.Current, DataRowView).Row
        UserControl11.MyValue = dr.Item(1)
End Sub

Private Sub CheckForChanges()
        Dim dr As DataRow = CType(cm.Current, DataRowView).Row
        If dr.Item(1) <> UserControl11.Value Then
            dr.Item(1) = UserControl11.Value
        End If
End Sub
A little sample I found, see if this helps. :cool:
 
hi,

thanks for the response.

actually the problem is within the control itself. so for example, i compile and generate the .DLL file. i then create a new project and add the control to my new project. now on my project i create a dataset with datatables and datacolumns. on my form, i now place my control and bind the Value property to one of the DataColumn in my DataTable. now i use a storeproc for the update command and the tables has a LAST_UPDATE column which the storedproc assigns a value of the date today. so in my form, i have the controls set and i run it, it displays the records. i then click the next record on my BindingNavigator and click Save. when i check the records back-end, the first 2 records where updated upon checking the LAST_UPDATE column which then lead me to trace the rowState of the records (in my earlier post i showed an example of how i did this). so basically what the control was doing is that it keeps returning a "Modified" rowState to all navigated records.

but now i was able to find a way to avoid this. the control does not return "Modified" all the time anymore. the problem now is that it returns "Unchanged" all the time even if i made changes to the records unlike before.

i'm thinking that it may just be a problem with the Event i created. this is the part where i need help i guess.
 
Back
Top