Disable editing in data-bound controls

Hello,

I have a .NET 2.0 windows forms application with some TextBox controls and a BindingNavigator on the form that are bound to a BindingSource. The BindingSource has a DataView as the DataSource, and when I change the DataView AllowNew or AllowRemove properties to true or false, the BindingNavigator immediately reflects the changes, enabling or disabling the Add and Delete buttons. However, I can always write on the TextBox controls, independently of the value in the AllowEdit property.
Does anyone know how to enable and disable the editing on the controls in this situation
Thanks in advance for any help given.

Regards,
paulo


Answer this question

Disable editing in data-bound controls

  • Julie Brazier

    You may be able to automate the process by enumerating the controls.  You could limit disabling to certain types.  For instance, to disable all TextBoxes on a form:



    foreach (Control ctl in this.Controls)
    {
       if (ctl is TextBox)
          ctl.Enabled =
    false;
    }

     


    Good luck...

  • coozdrm

    if you can fit them into a group box, you could iterate through the textboxes in the group box, too.
  • lauch

    Hi and thank you for all your replys.

    Indeed, I wanted to stay away from enabling or disabling controls because I can have a control that I want always disabled, and when I enable or disable the others I want that specific one to remain disabled in any case. If there was an automatic method that was a better solution. Why do the controls fail to change to read-only when AllowEdit is false
    Anyway, I can iterate all databound controls using:

    for (int i = 0; i < bindingSource.CurrencyManager.Bindings.Count; i++)bindingSource.CurrencyManager.Bindings[ i ].Control.Enabled = false;

    Thanks.

    Regards,
    paulo

  • Stephen Weatherford MS

    Sounds like you need to do [textbox].enabled = false, but I'm assuming you want to avoid that for some reason (maybe it gets tedious for every textbox)
  • Disable editing in data-bound controls