Results 1 to 4 of 4

Thread: datagridview bound to bindingsource: displaying both old values and the new values

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    datagridview bound to bindingsource: displaying both old values and the new values

    Hi,

    I bound datagridview to bindingsource. The bindingsource has its datasource a collection Called Rates which is a collection of Rate items.

    The rate object has a property called VALUE. I need the user to be able to edit different values for the collection; However, on the datagridview, i need to keep displaying on one column called OldValue the existing Rate value before the change while allowing the user to edit that value in a second column called NewValue.

    In other terms, One colum OldValue will keep the existing values from the DB before being edited by the user, this column OldValue is readonly. Another column NewValue will display, the 1st time, the old value from the DB (similar to OldValue when we do the Fetch from the DB), but it`s editable column, so the user can modify each value in the column NewValue without overriding OldValue cells.

    For now, I bound both columns OldValue and NewValue to the same property VALUE of the Rate object,

    The problem I am having now is: when the user edits NewValue column, the same newvalue is copied to the first column : OldValue. I want the OldValue column to keep displaying the oldvalues instead of the new ones.

    Do you know pls how I can do that ?

    Thank you
    Thanks a lot for your help.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: datagridview bound to bindingsource: displaying both old values and the new value

    1. You may want to set the LastValue" column in the datagridview to readonly (so that the user can't change it)
    2. After you populate the datatable and right before you bind it to your datagridview, you use addhandler to add the datatable.columnchanging event handler to it.

    3. Handle the datatable.columnchanging event of the datatable bound to your datagridview
    Code:
    Private Sub dt_ColumnChanging(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs)
            If e.Column.ColumnName = "CurrentValue" Then
                e.Row("LastValue") = e.Row("CurrentValue")
            End If
        End Sub
    Upon re-reading your post, I have a question: What type is the Rate object?
    If it's a datatable, you need to add another column to it to store the OldValue, and then proceed as posted above.

    If it's a custom type, you need to add another property to it for the OldValue. In the setter of the NewValue property, you simply assign the current value to OldValue before setting the passed in value to NewValue variable.

    Code:
    private _OldValue as sometype
    Public ReadOnly Property OldValue() as sometype
          Get    
               Return _OldValue
          End Get
    End Property
      
    
    Private _NewValue as sometype
    Public Property NewValue() as sometype
        Get
             return _NewValue
        End Get
        Set(Byval value as sometype)
             OldValue = _NewValue
            _NewValue = value
        End Set
    End Property
    Last edited by stanav; Mar 23rd, 2011 at 01:50 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    Re: datagridview bound to bindingsource: displaying both old values and the new value

    Thank u,

    The reason I did not use newvalue, oldvalue properties in my Rate object is there is another requirement where the number of columns for which we need to keep oldvalue and newvalue is about 5 or 6 columns. I mean we would ve: oldValue1, NewValue1, oldValue2, NewValue2....oldValue6, NewValue6.



    This solution does not seem to me as a best practice, I mean we can t alter the Rate object and design it to fit and accomodate the look of the UI, it should be the other way around, right ?

    Anyway, here is what I did for now as a solution:

    I create the column OldValue which is UNBOUND to the bindingsource. The column NewValue is BOUND to the binding source.

    During the 1st load of the page: after databindingcomplete, I copy the value of the column NewValue into the unbound column OldValue, that s done only during the first load of the form, this way, subsequent changes of the column NewValue does not affect the readonly OldValue.



    I hope this is good solution, let me know if u have other solutions

    Thank u
    Thanks a lot for your help.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: datagridview bound to bindingsource: displaying both old values and the new value

    So let's say when the form starts, the oldvalue = newvalue = 100 for example. The user changes the newvalue to 200. At this point, the oldvalue is still 100 and newvalue is 200. All is good. And then the user later changes his mind again and wants to make the newvalue = 300. Shouldn't the oldvalue now is 200 instead of staying fixed at 100 regardless of how many times newvalue gets changed?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width