Home > OS >  .NET get BindingSource value from Control
.NET get BindingSource value from Control

Time:09-28

I have some controls on a tablelayout and every control has a bound value (coming from a DataTable and bound with an BindingSource). My application hides some controls after interaction and I want the value of every hidden control set to null or DBNull.Value if the bound control turned invisible.

Basically I have a loop which goes through all controls in my tablelayout.

foreach (System.Windows.Forms.Control c in tablelayout.Controls)
{
    if (c.Visible == false && c.DataBindings.Count > 0)
    {
        Binding binding = c.DataBindings[0]; // only one binding per control
        // here I would do something like (object)binding.Value = null; 
    }
}

Is this possible? My last solution would be to just manually change the value of every control I turned invisible...

CodePudding user response:

My solution:

foreach (System.Windows.Forms.Control c in tablelayout.Controls)
{
    if (c.Visible == false && c.DataBindings.Count > 0)
    {
        c.Text = null; // sets the Text to null
        bindingSource.EndEdit(); // redraws the BindingSource, so its committing the new value
    }
}
  • Related