Selectively making bound controls Read Only

I have bound a dataview to some controls (textbox, rtf, etc...) and some records need to be read only based on some of my own logic... now, whenever the position changes, I can determine if the row can be edited, but I need to block editing in the UI as well. My first thought was to toggle the "AllowEdit" property of the DataView, but that doesn't seem to have any effect on my bound controls. I had only used AllowEdit with DataGrids in the past, so I am not sure if it is even supposed to have an effect in other binding situations.

Now, I could easily go through the bound controls and set ReadOnly = True, but I was hoping for a more centralized method that wouldn't involve changes whenever I added a new bound control.

Thoughts  Advice


Answer this question

Selectively making bound controls Read Only

  • Piocon

    Actually, you _could_ do this through the Format event, but it may not gain you anything, dpending on how your code's set up.  The sender argument of the event is the Binding, and you can get the Control off that.

    If you do this in the Format event, you'll want to make sure to declare your Binding, then add the event handler, then add the binding (if you add the binding directly, you'll miss the first Format event).  The other drawback is that you'll have to add handlers to all the bindings where you want to control the readonly-ness.

    The advantage of using the Format event, though, is that it's more flexible: if you have controls that are going to be bound to a certain field, then bound to something different, then not bound at all, you don't have to keep track of it: your bindings can do it for you.

    -Scott

  • J.R. Odden

    Yep, that would work... and a bit more 'dynamic' than anything I was considering. Good enough then, but I was hoping for something at the binding or data view layer itself :(
  • le_sloth

    sender is the actual Binding   Ok, dang, good one, Scott!  I thought it just passed the value through for some reason...guess I should've tried it, because if it passes the binding, then yes, that is definitely the way to go...good catch, Scott!  :) 
  • R Phillips

    Yah, I was too, but after looking at the Format Event of the Binding Class, it seemed hopeless from that angle as the Format Method only pulls in the current value, the ending value (that you can change) and the desired type, so that didn't seem very promising.

    Good luck!  :) 

  • gofrm

    Hey Duncan, good to see you around!  :) 

    One thought I have off the top of my head is to setup a Method to handle the BindingManagerBase.PositionChanged, then in that Method check to see if the current row is readonly or not and then loop through BindingManagerBase.Bindings and change the ReadOnly or Enabled Properties accordingly for all the controls (it's unfortunate they don't all have ReadOnly Properties like the TextBox does...a bunch of us on here have actually requested this a few times here on the forum wishlist)

    So maybe something like this Method would work...


    Private Sub MyBindingManager_PositionChanged(ByVal sender As Object, ByVal e As EventArgs)
         Dim Enabled As Boolean = MyVariableThatSaysIfIShouldBeAbleToEditTheControls
         For Each CurBinding As Binding In MyBindingManager.Bindings
              If TypeOf CurBinding.Control Is TextBox Then
                   DirectCast(CurBinding.Control, TextBox).ReadOnly = Enabled
              Else
                   CurBinding.Control.Enabled = Enabled
              End If
         Next
    End Sub


    just pseudo code and an idea...haven't actually tried it.

  • Selectively making bound controls Read Only