.NET 从控件获取 BindingSource 值

.NET get BindingSource value from Control

我在表格布局上有一些控件,每个控件都有一个绑定值(来自 DataTable 并与 BindingSource 绑定)。我的应用程序在交互后隐藏了一些控件,如果绑定控件变为不可见,我希望将每个隐藏控件的值设置为 nullDBNull.Value

基本上我有一个循环遍历我的表格布局中的所有控件。

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; 
    }
}

这可能吗?我最后的解决方案是手动更改我隐藏的每个控件的值...

我的解决方案:

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
    }
}