Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

DataGridView - in a real bind

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel Manes - 25 Jul 2006 20:37 GMT
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
Phil - 25 Jul 2006 23:19 GMT
>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
[quoted text clipped - 3 lines]
>
>-Dan

Have you tried the EditingControlShowing event?
Daniel Manes - 26 Jul 2006 02:24 GMT
> 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
Phil - 26 Jul 2006 10:13 GMT
>> Have you tried the EditingControlShowing event?
>
[quoted text clipped - 5 lines]
>
>-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
Daniel Manes - 28 Jul 2006 02:12 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.