DataGridView - in a real bind

D

Daniel Manes

Take one DataGridView, add one column that contains cities, add another
that contains countries, add another that contains locales (states,
provinces, territories, etc.).

Now, make the Country and Locale columns ComboBox columns, and bind the
Country column to a Country BindingSource and the Locale column to a
Locale BindingSource.

Now run it. Pretend you are a user trying to add a new record. You
enter "San Diego" for the city, you select "United States" for the
Country. Then you go to select a state but the Locale column has every
locale of every country (almost 4,000 items), not just the 50 states.

Not good.

So, add an event handler that places a filter on the BindingSource to
show only locales belonging to the selected country whenever the user
clicks or enters a Locale cell in the DataGridView.

Run it again. The whole thing goes bananas. "Invalid cell" errors all
over the place. I think this is because the filter applies to the
*whole* Locale column, not just the ComboBox for the cell begin edited.
So if one of the rows contains Berlin but all the combo boxes in the
Locale column are filtered for United States, the validator gets angry.

So this is where I could use some help/advice. Is there any way to set
a filter for just the one cell (rather than the whole column)? Maybe
give that cell its own BindingSource?

A free box of my eternal gratitude to anyone who can help :)

-Dan
 
P

Phil

So this is where I could use some help/advice. Is there any way to set
a filter for just the one cell (rather than the whole column)? Maybe
give that cell its own BindingSource?

A free box of my eternal gratitude to anyone who can help :)

-Dan

Have you tried the EditingControlShowing event?
 
D

Daniel Manes

Phil said:
Have you tried the EditingControlShowing event?

Hey Phil,

Thanks. Just checked out EditingControlShowing but couldn't figure out
a way to set the bindings on the control (there's a property for
DataBindings but it's read-only).

-Dan
 
P

Phil

Hey Phil,

Thanks. Just checked out EditingControlShowing but couldn't figure out
a way to set the bindings on the control (there's a property for
DataBindings but it's read-only).

-Dan

Hmm...I haven't actually done this but it might be worth a shot.

1. Create a BindingSource and associate your data with it.

2. Set the DataGridViewComboBoxColumn's data source to be the
BindingSource. These 2 steps have then setup the *whole list* of data
that the combo can show.

3. Bind the combo box column to the appropriate property on your
object (ie the one you want to set/get) using the DataPropertyName
property.

4. On the EditingControlShowing event, set the Filter property of the
BindingSource to restrict the items displayed in the combo box. It
takes a SQL-like expression syntax.



Fallback: If this doesn't work, you might consider simply using a
non-bound combo column and populating its list within the
EditingControlShowing event, or change the UI to have a little button
in an image column, and use that to pop up a form.


I'm currently learning data binding myself and I know how tricky
things can be...when it works it's great but when it doesn't figuring
out what is wrong can be a hard slog. [Aside, not directly relevant to
your situation: One thing I learnt this morning is make sure you set
properties like DisplayMember and ValueMember *before* you associate
the DataSource otherwise the control just ignores them. The code looks
right, compiles and runs fine but just doesn't do what you expect....]

Phil
 
D

Daniel Manes

Hey Phil,

Thanks again for the help...think I got the filtering problem solved. I
combined the CountryID and LocaleID into a single column, which then
allowed me to set the DisplayMember to the name of the locale and the
ValueMember to the new combined ID.
I'm currently learning data binding myself and I know how tricky
things can be...when it works it's great but when it doesn't figuring
out what is wrong can be a hard slog.

Hehe, tell me about it. I'm currently trying to figure out which of the
over 200 (!) DataGridView events to use to process the result when the
user selects an item from the combo box. When I use CellLeave, the
value of the cell is DBNull even though I definitely set the ComboBox
to a real value. I would have thought the value would have been set by
the time CellLeave is raised but apparently not.

Either that or something else is wrong :)

Take it easy,

-Dan
 

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