views:

47

answers:

1

Given a DataSet instance, is there any way I can find out which controls are bound to it's tables?

I want to stop everything binding to the DataSet, something like :

foreach (Control cont in dataset.ControlsBoundToMe)
{
    if (cont is DataGrid)
    {
        (cont as DataGrid).DataSource = null;
    }
}

Thanks

+2  A: 

Well I assume you're talking about Winforms controls.

Then, on every form, you can access the BindingContext property, that would give you a binding manager from a particular datasource. Once you have this manager, you can access its binding collection and iterating over it.

Pseudo code:

var bindingManager = BindingContext[myDataSet.Tables[0]];
foreach (Binding binding in bindingManager.Bindings)
{
    var dataGrid = binding.Control as DataGrid;
    if (dataGrid != null)
        dataGrid.DataSource = null;
}
Romain Verdier
I am using Winforms. Problem is I dont have access to the forms... This is occurring in the data access layer, so I only have the dataset in my class.
Mongus Pong
So I'm afraid you can't do that, unless you manually implement some sort of tracking system...
Romain Verdier
I had to implement the tracking system. Thanks.
Mongus Pong