Getting a control's data bindings?

D

David Veeneman

How do I get the data bindings for a control that is bound at design time?

I'm binding a DataGridView control to a data source at design time, using a
BindingSource control. I set the grid's DataSource property to the
BindingSource control, which the Data Source Configuration Wizard created
for me.

The data binding works fine when I run the app, but I can't get a reference
to the DataGridView's data bindings.

At runtime, when I check the grid's DataBindings.Count, I get zero back--no
data bindings. That can't be, since the bound grid is displaying data, just
like it's supposed to. And I get this result whether I check in the form
constructor, the form load event handler, or at any time after that.

The form's designer code doesn't contain any calls to DataBindings.Add(),
and it doesn't declare any Binding objects. But still the application works.

What's going on here? Am I looking in the wrong place for the grid's
bindings collection? How do I get a reference to the individual bindings, so
that I can work with them the way that I would work with a Binding object
that I had declared in code?

Thanks in advance.
 
D

David Veeneman

I found my answer (thanks to Brian Noyes, author of "Data Binding With
Windows Forms 2.0)--a worthwhile read).

Only 'simple' data binding (labels, text boxes, and the like) creates
Binding objects. 'Complex' data binding (grids, list boxes, combos, and the
like) does not create them. So, when binding to a grid, there are no Binding
objects to get.

Brian's book has a fairly detailed discussion of how to work with the
DataGridView in data binding scenarios. The book is worth it for that
chapter alone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top